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

Commit ec658657 by Mick Hansen

Merge pull request #2452 from janmeier/fieldSameName

Support for attributes with the same field name as attribute name
2 parents 5f901b1d a297b02f
...@@ -565,7 +565,8 @@ module.exports = (function() { ...@@ -565,7 +565,8 @@ module.exports = (function() {
if (identifier) { if (identifier) {
for (attrName in identifier) { for (attrName in identifier) {
// Field name mapping // Field name mapping
if (self.Model.rawAttributes[attrName].field) { var rawAttribute = self.Model.rawAttributes[attrName];
if (rawAttribute.field && rawAttribute.field !== rawAttribute.fieldName) {
identifier[self.Model.rawAttributes[attrName].field] = identifier[attrName]; identifier[self.Model.rawAttributes[attrName].field] = identifier[attrName];
delete identifier[attrName]; delete identifier[attrName];
} }
...@@ -658,7 +659,7 @@ module.exports = (function() { ...@@ -658,7 +659,7 @@ module.exports = (function() {
Instance.prototype.reload = function(options) { Instance.prototype.reload = function(options) {
var self = this var self = this
, where = [ , where = [
this.QueryInterface.quoteTable(this.Model.name) + '.' + this.QueryInterface.quoteIdentifier(this.Model.primaryKeyAttribute) + '=?', this.QueryInterface.quoteTable(this.Model.name) + '.' + this.QueryInterface.quoteIdentifier(this.Model.primaryKeyField) + '=?',
this.get(this.Model.primaryKeyAttribute, {raw: true}) this.get(this.Model.primaryKeyAttribute, {raw: true})
]; ];
......
...@@ -210,6 +210,8 @@ module.exports = (function() { ...@@ -210,6 +210,8 @@ module.exports = (function() {
// Primary key convenience variables // Primary key convenience variables
this.primaryKeyAttributes = Object.keys(this.primaryKeys); this.primaryKeyAttributes = Object.keys(this.primaryKeys);
this.primaryKeyAttribute = this.primaryKeyAttributes[0]; this.primaryKeyAttribute = this.primaryKeyAttributes[0];
this.primaryKeyField = this.rawAttributes[this.primaryKeyAttribute].field || this.primaryKeyAttribute;
this.primaryKeyCount = this.primaryKeyAttributes.length; this.primaryKeyCount = this.primaryKeyAttributes.length;
this._hasPrimaryKeys = this.options.hasPrimaryKeys = this.hasPrimaryKeys = this.primaryKeyCount > 0; this._hasPrimaryKeys = this.options.hasPrimaryKeys = this.hasPrimaryKeys = this.primaryKeyCount > 0;
...@@ -716,7 +718,7 @@ module.exports = (function() { ...@@ -716,7 +718,7 @@ module.exports = (function() {
if (options.hooks) { if (options.hooks) {
return this.runHooks('beforeFindAfterExpandIncludeAll', options); return this.runHooks('beforeFindAfterExpandIncludeAll', options);
} }
}).then(function() { }).then(function() {
if (typeof options === 'object') { if (typeof options === 'object') {
if (options.include) { if (options.include) {
hasJoin = true; hasJoin = true;
...@@ -745,7 +747,7 @@ module.exports = (function() { ...@@ -745,7 +747,7 @@ module.exports = (function() {
if (options.hooks) { if (options.hooks) {
return this.runHooks('beforeFindAfterOptions', options); return this.runHooks('beforeFindAfterOptions', options);
} }
}).then(function() { }).then(function() {
return this.QueryInterface.select(this, this.getTableName(), options, Utils._.defaults({ return this.QueryInterface.select(this, this.getTableName(), options, Utils._.defaults({
type: QueryTypes.SELECT, type: QueryTypes.SELECT,
hasJoin: hasJoin, hasJoin: hasJoin,
...@@ -1279,11 +1281,14 @@ module.exports = (function() { ...@@ -1279,11 +1281,14 @@ module.exports = (function() {
return Utils._.omit(dao.dataValues, self._virtualAttributes); return Utils._.omit(dao.dataValues, self._virtualAttributes);
}); });
var rawAttribute;
// Map field names // Map field names
records.forEach(function(values) { records.forEach(function(values) {
for (var attr in values) { for (var attr in values) {
if (values.hasOwnProperty(attr)) { if (values.hasOwnProperty(attr)) {
if (self.rawAttributes[attr].field) { rawAttribute = self.rawAttributes[attr];
if (rawAttribute.field && rawAttribute.field !== rawAttribute.fieldName) {
values[self.rawAttributes[attr].field] = values[attr]; values[self.rawAttributes[attr].field] = values[attr];
delete values[attr]; delete values[attr];
} }
...@@ -1599,7 +1604,8 @@ module.exports = (function() { ...@@ -1599,7 +1604,8 @@ module.exports = (function() {
if (options.where) { if (options.where) {
var attributes = options.where var attributes = options.where
, attribute; , attribute
, rawAttribute;
if (options.where instanceof Utils.and || options.where instanceof Utils.or) { if (options.where instanceof Utils.and || options.where instanceof Utils.or) {
attributes = undefined; attributes = undefined;
...@@ -1612,7 +1618,8 @@ module.exports = (function() { ...@@ -1612,7 +1618,8 @@ module.exports = (function() {
if (attributes) { if (attributes) {
for (attribute in attributes) { for (attribute in attributes) {
if (Model.rawAttributes[attribute] && Model.rawAttributes[attribute].field) { rawAttribute = Model.rawAttributes[attribute];
if (rawAttribute && rawAttribute.field && rawAttribute.field !== rawAttribute.fieldName) {
attributes[Model.rawAttributes[attribute].field] = attributes[attribute]; attributes[Model.rawAttributes[attribute].field] = attributes[attribute];
delete attributes[attribute]; delete attributes[attribute];
} }
......
...@@ -879,7 +879,9 @@ module.exports = (function() { ...@@ -879,7 +879,9 @@ module.exports = (function() {
* A way of specifying attr = condition. * A way of specifying attr = condition.
* *
* The attr can either be an object taken from `Model.rawAttributes` (for example `Model.rawAttributes.id` or `Model.rawAttributes.name`). The * The attr can either be an object taken from `Model.rawAttributes` (for example `Model.rawAttributes.id` or `Model.rawAttributes.name`). The
* attribute should be defined in your model definition. The attribute can also be an object from one of the sequelize utility functions (`sequelize.fn`, `sequelize.col` et.c). * attribute should be defined in your model definition. The attribute can also be an object from one of the sequelize utility functions (`sequelize.fn`, `sequelize.col` etc.)
*
* For string attributes, use the regular `{ where: { attr: something }}` syntax. If you don't want your string to be escaped, use `sequelize.literal`.
* *
* @see {Model#find} * @see {Model#find}
* *
......
...@@ -134,6 +134,46 @@ describe(Support.getTestDialectTeaser("Model"), function () { ...@@ -134,6 +134,46 @@ describe(Support.getTestDialectTeaser("Model"), function () {
]); ]);
}); });
describe('field and attribute name is the same', function () {
beforeEach(function () {
return this.Comment.bulkCreate([
{ notes: 'Number one'},
{ notes: 'Number two'},
]);
});
it('bulkCreate should work', function () {
return this.Comment.findAll().then(function (comments) {
expect(comments[0].notes).to.equal('Number one');
expect(comments[1].notes).to.equal('Number two');
});
});
it('find with where should work', function () {
return this.Comment.findAll({ where: { notes: 'Number one' }}).then(function (comments) {
expect(comments).to.have.length(1);
expect(comments[0].notes).to.equal('Number one');
});
});
it('reload should work', function () {
return this.Comment.find(1).then(function (comment) {
return comment.reload();
});
});
it('save should work', function () {
return this.Comment.create({ notes: 'my note' }).then(function (comment) {
comment.notes = 'new note';
return comment.save();
}).then(function (comment) {
return comment.reload();
}).then(function (comment) {
expect(comment.notes).to.equal('new note');
});
});
});
it('should create, fetch and update with alternative field names from a simple model', function () { it('should create, fetch and update with alternative field names from a simple model', function () {
var self = this; var self = this;
...@@ -544,4 +584,4 @@ describe(Support.getTestDialectTeaser("Model"), function () { ...@@ -544,4 +584,4 @@ describe(Support.getTestDialectTeaser("Model"), function () {
}); });
}); });
}); });
}); });
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!