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

Commit 2d3360cd by Jan Aagaard Meier

bug(scope/include) Don't add an aliasses primary key to include.attributes if it…

…'s already there. Closes #4127
1 parent 12d4b8cd
......@@ -4,6 +4,7 @@
- [FIXED] Fix save to be noop when nothing changed
- [FIXED] Call `conformOptions` on default scope [#4157](https://github.com/sequelize/sequelize/issues/4157)
- [FIXED] Call `conformOptions` on scopes returned by functions [#3991](https://github.com/sequelize/sequelize/issues/3991)
- [FIXED] Calling `validateIncludedElements` should not add an aliassed primary key multiple times [#4127](https://github.com/sequelize/sequelize/issues/4127)
# 3.4.1
- [FIXED] Fix belongs-to-many `countAssociations` - ambigious id when through model has id
......
......@@ -516,9 +516,15 @@ validateIncludedElement = function(include, tableNames, options) {
include.originalAttributes = include.attributes.slice(0);
if (include.attributes.length) {
include.model.primaryKeyAttributes.forEach(function (attr) {
if (include.attributes.indexOf(attr) === -1) {
include.attributes.unshift(attr);
_.each(include.model.primaryKeys, function (attr, key) {
// Include the primary key if its not already take - take into account that the pk might be aliassed (due to a .field prop)
if (!_.any(include.attributes, function (includeAttr) {
if (attr.field !== key) {
return Array.isArray(includeAttr) && includeAttr[0] === attr.field && includeAttr[1] === key;
}
return includeAttr === key;
})) {
include.attributes.unshift(key);
}
});
}
......
......@@ -32,6 +32,12 @@ describe(Support.getTestDialectTeaser('Model'), function() {
title: Sequelize.STRING
});
this.Company = this.sequelize.define('Company', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
field: 'field_id'
},
name: Sequelize.STRING
});
......@@ -41,6 +47,27 @@ describe(Support.getTestDialectTeaser('Model'), function() {
this.Company.Owner = this.Company.belongsTo(this.User, {as: 'Owner', foreignKey: 'ownerId'});
});
describe('attributes', function () {
it('should not inject the aliassed PK again, if its already there', function () {
var options = Sequelize.Model.$validateIncludedElements({
model: this.User,
include: [
{
model: this.Company,
attributes: ['name']
}
]
});
expect(options.include[0].attributes).to.deep.equal([['field_id', 'id'], 'name']);
options = Sequelize.Model.$validateIncludedElements(options);
// Calling validate again shouldn't add the pk again
expect(options.include[0].attributes).to.deep.equal([['field_id', 'id'], 'name']);
});
});
describe('duplicating', function () {
it('should tag a hasMany association as duplicating: true if undefined', function () {
var options = Sequelize.Model.$validateIncludedElements({
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!