不要怂,就是干,撸起袖子干!

Commit ff320e0f by Erik Seliger Committed by Felix Becker

V4 fix belongsto foreignkeys (#6650)

1 parent 70e053cb
# Future # Future
- [FIXED] Issues with `createFunction` and `dropFunction` (PostgresSQL) - [FIXED] Issues with `createFunction` and `dropFunction` (PostgresSQL)
- [FIXED] Issue with belongsTo association and foreign keys [#6400](https://github.com/sequelize/sequelize/issues/6400)
# 4.0.0-2 # 4.0.0-2
- [ADDED] include now supports string as an argument (on top of model/association), string will expand into an association matched literally from Model.associations - [ADDED] include now supports string as an argument (on top of model/association), string will expand into an association matched literally from Model.associations
......
...@@ -1445,14 +1445,16 @@ const QueryGenerator = { ...@@ -1445,14 +1445,16 @@ const QueryGenerator = {
includeIgnoreAttributes: false includeIgnoreAttributes: false
}, topInclude.through.model); }, topInclude.through.model);
} else { } else {
const isBelongsTo = topInclude.association.associationType === 'BelongsTo';
const join = [
this.quoteTable(topParent.model.name) + '.' + this.quoteIdentifier(isBelongsTo ? topInclude.association.identifierField : topParent.model.primaryKeyAttributes[0]),
this.quoteIdentifier(topInclude.model.name) + '.' + this.quoteIdentifier(isBelongsTo ? topParent.model.primaryKeyAttributes[0] : topInclude.association.identifierField)
].join(' = ');
query = this.selectQuery(topInclude.model.tableName, { query = this.selectQuery(topInclude.model.tableName, {
attributes: [topInclude.model.primaryKeyAttributes[0]], attributes: [topInclude.model.primaryKeyAttributes[0]],
include: topInclude.include, include: topInclude.include,
where: { where: {
$join: this.sequelize.asIs([ $join: this.sequelize.asIs(join)
this.quoteTable(topParent.model.name) + '.' + this.quoteIdentifier(topParent.model.primaryKeyAttributes[0]),
this.quoteIdentifier(topInclude.model.name) + '.' + this.quoteIdentifier(topInclude.association.identifierField)
].join(' = '))
}, },
limit: 1, limit: 1,
includeIgnoreAttributes: false includeIgnoreAttributes: false
......
...@@ -836,3 +836,96 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() { ...@@ -836,3 +836,96 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() {
}); });
}); });
}); });
describe('Association', function() {
it('should set foreignKey on foreign table', function () {
const Mail = this.sequelize.define('mail', {}, { timestamps: false });
const Entry = this.sequelize.define('entry', {}, { timestamps: false });
const User = this.sequelize.define('user', {}, { timestamps: false });
Entry.belongsTo(User, { as: 'owner', foreignKey: { name: 'ownerId', allowNull: false } });
Entry.belongsTo(Mail, {
as: 'mail',
foreignKey: {
name: 'mailId',
allowNull: false
}
});
Mail.belongsToMany(User, {
as: 'recipients',
through: 'MailRecipients',
otherKey: {
name: 'recipientId',
allowNull: false
},
foreignKey: {
name: 'mailId',
allowNull: false
},
timestamps: false
});
Mail.hasMany(Entry, {
as: 'entries',
foreignKey: {
name: 'mailId',
allowNull: false
}
});
User.hasMany(Entry, {
as: 'entries',
foreignKey: {
name: 'ownerId',
allowNull: false
}
});
return this.sequelize.sync({ force: true })
.then(() => User.create({}))
.then(() => Mail.create({}))
.then(mail =>
Entry.create({ mailId: mail.id, ownerId: 1 })
.then(() => Entry.create({ mailId: mail.id, ownerId: 1 }))
// set recipients
.then(() => mail.setRecipients([1]))
)
.then(() => Entry.findAndCount({
offset: 0,
limit: 10,
order: [['id', 'DESC']],
include: [
{
association: Entry.associations.mail,
include: [
{
association: Mail.associations.recipients,
through: {
where: {
recipientId: 1
}
},
required: true
}
],
required: true
}
]
})).then(result => {
expect(result.count).to.equal(2);
expect(result.rows[0].get({ plain: true })).to.deep.equal(
{
id: 2,
ownerId: 1,
mailId: 1,
mail: {
id: 1,
recipients: [{
id: 1,
MailRecipients: {
mailId: 1,
recipientId: 1
}
}]
}
}
);
});
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!