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

Commit cc45eec1 by Mick Hansen

fix issue where equality could not be properly checked if not including id in at…

…tributes on main table while including
1 parent 3aef3958
......@@ -453,6 +453,13 @@ module.exports = (function() {
hasJoin = true
validateIncludedElements.call(this, options, tableNames)
if (options.attributes) {
if (options.attributes.indexOf(this.primaryKeyAttribute) === -1) {
options.originalAttributes = options.attributes;
options.attributes = [this.primaryKeyAttribute].concat(options.attributes);
}
}
}
// whereCollection is used for non-primary key updates
......
......@@ -245,7 +245,7 @@ module.exports = (function() {
includeNames: this.options.includeNames,
includeMap: this.options.includeMap,
includeValidated: true,
attributes: this.options.attributes,
attributes: this.options.originalAttributes || this.options.attributes,
raw: true
})
} else if (this.options.hasJoinTableModel === true) {
......
......@@ -624,7 +624,8 @@ module.exports = (function() {
includeMap: options.includeMap,
hasSingleAssociation: options.hasSingleAssociation,
hasMultiAssociation: options.hasMultiAssociation,
attributes: options.attributes
attributes: options.attributes,
originalAttributes: options.originalAttributes
})
return queryAndEmit.call(this, [sql, factory, queryOptions], 'select')
......
......@@ -1410,6 +1410,87 @@ describe(Support.getTestDialectTeaser("Include"), function () {
})
})
// Test case by @eshell
it('should be possible not to include the main id in the attributes', function (done) {
var Member = this.sequelize.define('Member', {
id: {
type: Sequelize.BIGINT,
primaryKey: true,
autoIncrement: true
},
email:{
type: Sequelize.STRING,
unique: true,
allowNull: false,
validate:{
isEmail: true,
notNull: true,
notEmpty: true
}
},
password: Sequelize.STRING
});
var Album = this.sequelize.define('Album', {
id: {
type: Sequelize.BIGINT,
primaryKey: true,
autoIncrement: true
},
title: {
type: Sequelize.STRING(25),
allowNull: false
}
});
Album.belongsTo(Member);
Member.hasMany(Album);
this.sequelize.sync({force: true}).done(function (err) {
expect(err).not.to.be.ok;
var members = []
, albums = []
, memberCount = 20
for(var i = 1;i <= memberCount;i++) {
members.push({
id: i,
email: 'email'+i+'@lmu.com',
password: 'testing'+i,
});
albums.push({
title: 'Album'+i,
MemberId: i
});
}
Member.bulkCreate(members).done(function (err) {
expect(err).not.to.be.ok;
Album.bulkCreate(albums).done(function (err) {
expect(err).not.to.be.ok;
Member.findAll({
attributes: ['email'],
include:[
{
model:Album
}
]
}).done(function (err, members) {
expect(err).not.to.be.ok;
expect(members.length).to.equal(20);
members.forEach(function (member) {
expect(member.get('id')).not.to.be.ok;
expect(member.albums.length).to.equal(1);
});
done();
});
});
});
});
});
it('should be possible to use limit and a where on a hasMany with additional includes', function (done) {
var self = this
this.fixtureA(function () {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!