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

Commit a5fc0996 by Erik Seliger Committed by Felix Becker

V3 fix belongsto foreignkeys (#6651)

1 parent bb1b4127
# Future # Future
- [FIXED] Accept dates as string while using `typeValidation` [#6453](https://github.com/sequelize/sequelize/issues/6453) - [FIXED] Accept dates as string while using `typeValidation` [#6453](https://github.com/sequelize/sequelize/issues/6453)
- [FIXED] - ORDER clause was not included in subquery if `order` option value was provided as plain string (not as an array value) - [FIXED] - ORDER clause was not included in subquery if `order` option value was provided as plain string (not as an array value)
- [FIXED] Issue with belongsTo association and foreign keys [#6400](https://github.com/sequelize/sequelize/issues/6400)
# 3.24.1 # 3.24.1
- [FIXED] Add `parent`, `original` and `sql` properties to `UniqueConstraintError` - [FIXED] Add `parent`, `original` and `sql` properties to `UniqueConstraintError`
......
...@@ -1305,14 +1305,16 @@ var QueryGenerator = { ...@@ -1305,14 +1305,16 @@ var QueryGenerator = {
includeIgnoreAttributes: false includeIgnoreAttributes: false
}, topInclude.through.model); }, topInclude.through.model);
} else { } else {
var isBelongsTo = topInclude.association.associationType === 'BelongsTo';
var join = [
self.quoteTable(topParent.model.name) + '.' + self.quoteIdentifier(isBelongsTo ? topInclude.association.identifierField : topParent.model.primaryKeyAttributes[0]),
self.quoteIdentifier(topInclude.model.name) + '.' + self.quoteIdentifier(isBelongsTo ? topParent.model.primaryKeyAttributes[0] : topInclude.association.identifierField)
].join(' = ');
$query = self.selectQuery(topInclude.model.tableName, { $query = self.selectQuery(topInclude.model.tableName, {
attributes: [topInclude.model.primaryKeyAttributes[0]], attributes: [topInclude.model.primaryKeyAttributes[0]],
include: topInclude.include, include: topInclude.include,
where: { where: {
$join: self.sequelize.asIs([ $join: self.sequelize.asIs(join)
self.quoteTable(topParent.model.name) + '.' + self.quoteIdentifier(topParent.model.primaryKeyAttributes[0]),
self.quoteIdentifier(topInclude.model.name) + '.' + self.quoteIdentifier(topInclude.association.identifierField)
].join(' = '))
}, },
limit: 1, limit: 1,
includeIgnoreAttributes: false includeIgnoreAttributes: false
......
...@@ -836,3 +836,97 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() { ...@@ -836,3 +836,97 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() {
}); });
}); });
}); });
describe('Association', function() {
it('should set foreignKey on foreign table', function () {
var self = this;
var Mail = self.Mail = this.sequelize.define('mail', {}, { updatedAt: false, createdAt: false });
var Entry = self.Entry = this.sequelize.define('entry', {}, { updatedAt: false, createdAt: false });
var User = self.User = this.sequelize.define('user', {}, { updatedAt: false, createdAt: 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
}
});
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(function() { return self.User.create({}); })
.then(function() { return self.Mail.create({}); })
.then(function(mail) {
return self.Entry.create({ mailId: mail.id, ownerId: 1 })
.then(function() { return self.Entry.create({ mailId: mail.id, ownerId: 1 }); })
// set recipients
.then(function() { return mail.setRecipients([1]); });
})
.then(function() {
return self.Entry.findAndCount({
offset: 0,
limit: 1,
order: [['id', 'DESC']],
include: [
{
association: self.Entry.associations.mail,
include: [
{
association: self.Mail.associations.recipients,
through: {
where: {
recipientId: 1
}
},
required: true
}
],
required: true
}
]
});
}).then(function(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!