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

Commit 9b87cb86 by Justin Kalland Committed by Sushant

fix(query): handle undefined field on unique constraint error (#10016) (#10018)

1 parent 5db576a2
...@@ -56,7 +56,8 @@ AbstractDialect.prototype.supports = { ...@@ -56,7 +56,8 @@ AbstractDialect.prototype.supports = {
parser: false, parser: false,
concurrently: false, concurrently: false,
type: false, type: false,
using: true using: true,
functionBased: false
}, },
joinTableDependent: true, joinTableDependent: true,
groupedLimit: true, groupedLimit: true,
......
...@@ -126,9 +126,9 @@ class AbstractQuery { ...@@ -126,9 +126,9 @@ class AbstractQuery {
} }
getUniqueConstraintErrorMessage(field) { getUniqueConstraintErrorMessage(field) {
let message = field + ' must be unique'; let message = field ? field + ' must be unique' : 'Must be unique';
if (this.model) { if (field && this.model) {
for (const key of Object.keys(this.model.uniqueKeys)) { for (const key of Object.keys(this.model.uniqueKeys)) {
if (this.model.uniqueKeys[key].fields.indexOf(field.replace(/"/g, '')) >= 0) { if (this.model.uniqueKeys[key].fields.indexOf(field.replace(/"/g, '')) >= 0) {
if (this.model.uniqueKeys[key].msg) { if (this.model.uniqueKeys[key].msg) {
......
...@@ -38,7 +38,8 @@ PostgresDialect.prototype.supports = _.merge(_.cloneDeep(AbstractDialect.prototy ...@@ -38,7 +38,8 @@ PostgresDialect.prototype.supports = _.merge(_.cloneDeep(AbstractDialect.prototy
index: { index: {
concurrently: true, concurrently: true,
using: 2, using: 2,
where: true where: true,
functionBased: true
}, },
inserts: { inserts: {
onConflictDoNothing: ' ON CONFLICT DO NOTHING' onConflictDoNothing: ' ON CONFLICT DO NOTHING'
......
...@@ -28,7 +28,8 @@ SqliteDialect.prototype.supports = _.merge(_.cloneDeep(AbstractDialect.prototype ...@@ -28,7 +28,8 @@ SqliteDialect.prototype.supports = _.merge(_.cloneDeep(AbstractDialect.prototype
}, },
index: { index: {
using: false, using: false,
where: true where: true,
functionBased: true
}, },
transactionOptions: { transactionOptions: {
type: true type: true
......
...@@ -998,6 +998,26 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -998,6 +998,26 @@ describe(Support.getTestDialectTeaser('Model'), () => {
}); });
}); });
if (current.dialect.supports.index.functionBased) {
it("doesn't allow duplicated records with unique function based indexes", function () {
const User = this.sequelize.define('UserWithUniqueUsernameFunctionIndex', {
username: Sequelize.STRING,
email: {type: Sequelize.STRING, unique: true}
});
return User.sync({force: true}).then(() => {
const tableName = User.getTableName();
return this.sequelize.query('CREATE UNIQUE INDEX lower_case_username ON "' + tableName + '" ((lower(username)))');
}).then(() => {
return User.create({username: 'foo'});
}).then(() => {
return User.create({username: 'foo'});
}).catch(Sequelize.UniqueConstraintError, err => {
expect(err).to.be.ok;
});
});
}
it('raises an error if created object breaks definition contraints', function() { it('raises an error if created object breaks definition contraints', function() {
const UserNull = this.sequelize.define('UserWithNonNullSmth', { const UserNull = this.sequelize.define('UserWithNonNullSmth', {
username: { type: Sequelize.STRING, unique: true }, username: { type: Sequelize.STRING, unique: true },
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!