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

Commit 6b7e3e0a by Mick Hansen

add field mapping support for and()/or(), fixes #2237

1 parent 7aa4c0ea
Showing with 62 additions and 13 deletions
......@@ -1575,10 +1575,24 @@ module.exports = (function() {
}
if (options.where) {
for (var attr in options.where) {
if (Model.rawAttributes[attr] && Model.rawAttributes[attr].field) {
options.where[Model.rawAttributes[attr].field] = options.where[attr];
delete options.where[attr];
var attributes = options.where
, attribute;
if (options.where instanceof Utils.and || options.where instanceof Utils.or) {
attributes = undefined;
options.where.args = options.where.args.map(function (where) {
return mapFieldNames({
where: where
}, Model).where;
});
}
if (attributes) {
for (attribute in attributes) {
if (Model.rawAttributes[attribute] && Model.rawAttributes[attribute].field) {
attributes[Model.rawAttributes[attribute].field] = attributes[attribute];
delete attributes[attribute];
}
}
}
}
......
......@@ -66,10 +66,10 @@ describe(Support.getTestDialectTeaser("Model"), function () {
type: DataTypes.STRING,
field: 'comment_text'
},
notes: {
type: DataTypes.STRING,
field: 'notes'
}
notes: {
type: DataTypes.STRING,
field: 'notes'
}
}, {
tableName: 'comments',
timestamps: false
......@@ -127,9 +127,9 @@ describe(Support.getTestDialectTeaser("Model"), function () {
comment_text: {
type: DataTypes.STRING
},
notes: {
type: DataTypes.STRING
}
notes: {
type: DataTypes.STRING
}
})
]);
});
......@@ -240,6 +240,24 @@ describe(Support.getTestDialectTeaser("Model"), function () {
});
});
it('should work with a where or', function () {
var self = this;
return this.User.create({
name: 'Foobar'
}).then(function () {
return self.User.find({
where: self.sequelize.or({
name: 'Foobar'
}, {
name: 'Lollerskates'
})
});
}).then(function (user) {
expect(user).to.be.ok;
});
});
it('should work with bulkCreate and findAll', function () {
var self = this;
return this.User.bulkCreate([{
......@@ -318,7 +336,25 @@ describe(Support.getTestDialectTeaser("Model"), function () {
expect(comment.get('notes')).to.equal('Barfoo');
});
});
it('should work with with an belongsTo association getter', function () {
var userId = Math.floor(Math.random() * 100000);
return Promise.join(
this.User.create({
id: userId
}),
this.Task.create({
user_id: userId
})
).spread(function (user, task) {
return [user, task.getUser()];
}).spread(function (userA, userB) {
expect(userA.get('id')).to.equal(userB.get('id'));
expect(userA.get('id')).to.equal(userId);
expect(userB.get('id')).to.equal(userId);
});
});
});
describe('types', function () {
......@@ -469,7 +505,6 @@ describe(Support.getTestDialectTeaser("Model"), function () {
test_value: {
type: Sequelize.INTEGER,
set: function(v) {
console.log("set called");
self.callCount++;
this.setDataValue('test_value', v+1);
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!