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

Commit c381625d by Sushant Committed by GitHub

Fixed: jsonb/json update or softdelete (#7499)

1 parent 18f63e3f
......@@ -67,6 +67,7 @@
- [ADDED] Added support for associations aliases in orders and groups. [#7425](https://github.com/sequelize/sequelize/issues/7425)
- [REMOVED] Removes support for `{raw: 'injection goes here'}` for order and group. [#7188](https://github.com/sequelize/sequelize/issues/7188)
- [FIXED] `showIndex` breaks with newline characters [#7492](https://github.com/sequelize/sequelize/pull/7492)
- [FIXED] Update or soft delete breaks when querying on `JSON/JSONB` [#7376](https://github.com/sequelize/sequelize/issues/7376) [#7400](https://github.com/sequelize/sequelize/issues/7400) [#7444](https://github.com/sequelize/sequelize/issues/7444)
## BC breaks:
- Model.validate instance method now runs validation hooks by default. Previously you needed to pass { hooks: true }. You can override this behavior by passing { hooks: false }
......
......@@ -362,7 +362,7 @@ const QueryGenerator = {
table: this.quoteTable(tableName),
values: values.join(','),
output: outputFragment,
where: this.whereQuery(where),
where: this.whereQuery(where, options),
tmpTable
};
......@@ -1930,7 +1930,7 @@ const QueryGenerator = {
let outerBinding;
let comparator = '=';
let field = options.field || options.model && options.model.rawAttributes && options.model.rawAttributes[key] || options.model && options.model.fieldRawAttributesMap && options.model.fieldRawAttributesMap[key];
let fieldType = options.type || field && field.type;
let fieldType = field && field.type || options.type;
if (key && typeof key === 'string' && key.indexOf('.') !== -1 && options.model) {
if (options.model.rawAttributes[key.split('.')[0]] && options.model.rawAttributes[key.split('.')[0]].type instanceof DataTypes.JSON) {
......
......@@ -241,7 +241,7 @@ const QueryGenerator = {
values.push(this.quoteIdentifier(key) + '=' + this.escape(value, modelAttributeMap && modelAttributeMap[key] || undefined, { context: 'UPDATE' }));
}
return `UPDATE ${this.quoteTable(tableName)} SET ${values.join(',')} ${this.whereQuery(where)}`;
return `UPDATE ${this.quoteTable(tableName)} SET ${values.join(',')} ${this.whereQuery(where, options)}`;
},
deleteQuery(tableName, where, options, model) {
......
......@@ -5,6 +5,7 @@ const DataTypes = require(__dirname + '/../../../lib/data-types');
const chai = require('chai');
const expect = chai.expect;
const sinon = require('sinon');
const current = Support.sequelize;
describe(Support.getTestDialectTeaser('Model'), () => {
describe('paranoid', () => {
......@@ -100,5 +101,62 @@ describe(Support.getTestDialectTeaser('Model'), () => {
expect(count).to.be.equal(1);
});
});
if (current.dialect.supports.JSON) {
describe('JSONB', () => {
before(function() {
this.Model = this.sequelize.define('Model', {
name: {
type: DataTypes.STRING
},
data: {
type: DataTypes.JSONB
},
deletedAt: {
type: DataTypes.DATE,
allowNull: true,
field: 'deleted_at'
}
}, {
paranoid: true,
timestamps: true,
deletedAt: 'deletedAt'
});
});
beforeEach(function() {
return this.Model.sync({ force: true });
});
it('should soft delete with JSONB condition', function() {
return this.Model.bulkCreate([{
name: 'One',
data: {
field: {
deep: true
}
}
}, {
name: 'Two',
data: {
field: {
deep: false
}
}
}]).then(() => this.Model.destroy({
where: {
data: {
field: {
deep: true
}
}
}
})).then(() => this.Model.findAll()).then(records => {
expect(records.length).to.equal(1);
expect(records[0].get('name')).to.equal('Two');
});
});
});
}
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!