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

Commit 10b5d628 by legomind Committed by Sushant

prevent renaming JSON & HSTORE fields #6752 (#6812)

1 parent fc8fdef0
......@@ -15,6 +15,7 @@
- [FIXED] `Increment` / `Decrement` properly maps to timestamp fields [#6296](https://github.com/sequelize/sequelize/issues/6296)
- [FIXED] Issue with overrriding custom methods with association mixins (all association methods are now exposed) [#6682](https://github.com/sequelize/sequelize/issues/6682)
- [ADDED] Support condition objects in utility functions [#6685](https://github.com/sequelize/sequelize/pull/6685)
- [FIXED] HSTORE and JSON fields being renamed when `options.field` is specified on a matching model attribute
## BC breaks:
- `DATEONLY` now returns string in `YYYY-MM-DD` format rather than `Date` type
......
......@@ -49,6 +49,7 @@ PostgresDialect.prototype.supports = _.merge(_.cloneDeep(AbstractDialect.prototy
GEOGRAPHY: true,
JSON: true,
JSONB: true,
HSTORE: true,
deferrableConstraints: true,
searchPath : true
});
......
......@@ -233,7 +233,10 @@ function mapWhereFieldNames(attributes, Model) {
delete attributes[attribute];
}
if (_.isPlainObject(attributes[attribute])) {
if (_.isPlainObject(attributes[attribute])
&& !(rawAttribute && (
rawAttribute.type instanceof DataTypes.HSTORE
|| rawAttribute.type instanceof DataTypes.JSON))) { // Prevent renaming of HSTORE & JSON fields
attributes[attribute] = mapOptionFieldNames({
where: attributes[attribute]
}, Model).where;
......
......@@ -332,6 +332,56 @@ if (dialect.match(/^postgres/)) {
});
});
it('should not rename hstore fields', function() {
const Equipment = this.sequelize.define('Equipment', {
grapplingHook: {
type: DataTypes.STRING,
field: 'grappling_hook'
},
utilityBelt: {
type: DataTypes.HSTORE
}
});
return Equipment.sync({ force: true }).then(() => {
return Equipment.findAll({
where: {
utilityBelt: {
grapplingHook: true
}
},
logging(sql) {
expect(sql).to.equal('Executing (default): SELECT "id", "grappling_hook" AS "grapplingHook", "utilityBelt", "createdAt", "updatedAt" FROM "Equipment" AS "Equipment" WHERE "Equipment"."utilityBelt" = \'"grapplingHook"=>"true"\';');
}
});
});
});
it('should not rename json fields', function() {
const Equipment = this.sequelize.define('Equipment', {
grapplingHook: {
type: DataTypes.STRING,
field: 'grappling_hook'
},
utilityBelt: {
type: DataTypes.JSON
}
});
return Equipment.sync({ force: true }).then(() => {
return Equipment.findAll({
where: {
utilityBelt: {
grapplingHook: true
}
},
logging(sql) {
expect(sql).to.equal('Executing (default): SELECT "id", "grappling_hook" AS "grapplingHook", "utilityBelt", "createdAt", "updatedAt" FROM "Equipment" AS "Equipment" WHERE ("Equipment"."utilityBelt"#>>\'{grapplingHook}\')::boolean = true;');
}
});
});
});
});
describe('range', function() {
......
......@@ -206,7 +206,7 @@ suite(Support.getTestDialectTeaser('SQL'), function() {
expect(function () {
type.validate('foobar');
}).to.throw(Sequelize.ValidationError, 'foobar is not a valid hstore');
}).to.throw(Sequelize.ValidationError, '"foobar" is not a valid hstore');
});
test('should return `true` if `value` is an hstore', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!