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

Commit ebec0cf9 by Sushant Committed by GitHub

fix(changeColumn): use engine defaults for foreign/unique key naming (#9890)

1 parent 27e44941
...@@ -260,8 +260,7 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator { ...@@ -260,8 +260,7 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator {
for (const attributeName in attributes) { for (const attributeName in attributes) {
const definition = attributes[attributeName]; const definition = attributes[attributeName];
if (definition.match(/REFERENCES/)) { if (definition.match(/REFERENCES/)) {
constraintString.push(_.template('<%= fkName %> FOREIGN KEY (<%= attrName %>) <%= definition %>', this._templateSettings)({ constraintString.push(_.template('FOREIGN KEY (<%= attrName %>) <%= definition %>', this._templateSettings)({
fkName: this.quoteIdentifier(attributeName + '_foreign_idx'),
attrName: this.quoteIdentifier(attributeName), attrName: this.quoteIdentifier(attributeName),
definition: definition.replace(/.+?(?=REFERENCES)/, '') definition: definition.replace(/.+?(?=REFERENCES)/, '')
})); }));
...@@ -279,7 +278,7 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator { ...@@ -279,7 +278,7 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator {
finalQuery += constraintString.length ? ' ' : ''; finalQuery += constraintString.length ? ' ' : '';
} }
if (constraintString.length) { if (constraintString.length) {
finalQuery += 'ADD CONSTRAINT ' + constraintString.join(', '); finalQuery += 'ADD ' + constraintString.join(', ');
} }
return _.template(query, this._templateSettings)({ return _.template(query, this._templateSettings)({
......
...@@ -141,10 +141,9 @@ class MySQLQueryGenerator extends AbstractQueryGenerator { ...@@ -141,10 +141,9 @@ class MySQLQueryGenerator extends AbstractQueryGenerator {
for (const attributeName in attributes) { for (const attributeName in attributes) {
let definition = attributes[attributeName]; let definition = attributes[attributeName];
if (definition.match(/REFERENCES/)) { if (definition.match(/REFERENCES/)) {
const fkName = this.quoteIdentifier(tableName + '_' + attributeName + '_foreign_idx');
const attrName = this.quoteIdentifier(attributeName); const attrName = this.quoteIdentifier(attributeName);
definition = definition.replace(/.+?(?=REFERENCES)/, ''); definition = definition.replace(/.+?(?=REFERENCES)/, '');
constraintString.push(`${fkName} FOREIGN KEY (${attrName}) ${definition}`); constraintString.push(`FOREIGN KEY (${attrName}) ${definition}`);
} else { } else {
attrString.push('`' + attributeName + '` `' + attributeName + '` ' + definition); attrString.push('`' + attributeName + '` `' + attributeName + '` ' + definition);
} }
...@@ -156,7 +155,7 @@ class MySQLQueryGenerator extends AbstractQueryGenerator { ...@@ -156,7 +155,7 @@ class MySQLQueryGenerator extends AbstractQueryGenerator {
finalQuery += constraintString.length ? ' ' : ''; finalQuery += constraintString.length ? ' ' : '';
} }
if (constraintString.length) { if (constraintString.length) {
finalQuery += 'ADD CONSTRAINT ' + constraintString.join(', '); finalQuery += 'ADD ' + constraintString.join(', ');
} }
return `ALTER TABLE ${this.quoteTable(tableName)} ${finalQuery};`; return `ALTER TABLE ${this.quoteTable(tableName)} ${finalQuery};`;
......
...@@ -294,7 +294,7 @@ class PostgresQueryGenerator extends AbstractQueryGenerator { ...@@ -294,7 +294,7 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
attrSql += _.template(query.replace('ALTER COLUMN', ''), this._templateSettings)({ attrSql += _.template(query.replace('ALTER COLUMN', ''), this._templateSettings)({
tableName: this.quoteTable(tableName), tableName: this.quoteTable(tableName),
query: 'ADD CONSTRAINT ' + this.quoteIdentifier(attributeName + '_unique_idx') + ' UNIQUE (' + this.quoteIdentifier(attributeName) + ')' query: 'ADD UNIQUE (' + this.quoteIdentifier(attributeName) + ')'
}); });
} }
...@@ -302,7 +302,7 @@ class PostgresQueryGenerator extends AbstractQueryGenerator { ...@@ -302,7 +302,7 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
definition = definition.replace(/.+?(?=REFERENCES)/, ''); definition = definition.replace(/.+?(?=REFERENCES)/, '');
attrSql += _.template(query.replace('ALTER COLUMN', ''), this._templateSettings)({ attrSql += _.template(query.replace('ALTER COLUMN', ''), this._templateSettings)({
tableName: this.quoteTable(tableName), tableName: this.quoteTable(tableName),
query: 'ADD CONSTRAINT ' + this.quoteIdentifier(attributeName + '_foreign_idx') + ' FOREIGN KEY (' + this.quoteIdentifier(attributeName) + ') ' + definition query: 'ADD FOREIGN KEY (' + this.quoteIdentifier(attributeName) + ') ' + definition
}); });
} else { } else {
attrSql += _.template(query, this._templateSettings)({ attrSql += _.template(query, this._templateSettings)({
......
...@@ -113,7 +113,7 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -113,7 +113,7 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
}); });
} }
//SQlite navitely doesnt support ALTER Foreign key //SQlite natively doesn't support ALTER Foreign key
if (dialect !== 'sqlite') { if (dialect !== 'sqlite') {
describe('should support foreign keys', () => { describe('should support foreign keys', () => {
beforeEach(function() { beforeEach(function() {
......
...@@ -62,9 +62,9 @@ if (current.dialect.name !== 'sqlite') { ...@@ -62,9 +62,9 @@ if (current.dialect.name !== 'sqlite') {
onDelete: 'cascade' onDelete: 'cascade'
}).then(sql => { }).then(sql => {
expectsql(sql, { expectsql(sql, {
mssql: 'ALTER TABLE [users] ADD CONSTRAINT [level_id_foreign_idx] FOREIGN KEY ([level_id]) REFERENCES [level] ([id]) ON DELETE CASCADE;', mssql: 'ALTER TABLE [users] ADD FOREIGN KEY ([level_id]) REFERENCES [level] ([id]) ON DELETE CASCADE;',
mysql: 'ALTER TABLE `users` ADD CONSTRAINT `users_level_id_foreign_idx` FOREIGN KEY (`level_id`) REFERENCES `level` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;', mysql: 'ALTER TABLE `users` ADD FOREIGN KEY (`level_id`) REFERENCES `level` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;',
postgres: 'ALTER TABLE "users" ADD CONSTRAINT "level_id_foreign_idx" FOREIGN KEY ("level_id") REFERENCES "level" ("id") ON DELETE CASCADE ON UPDATE CASCADE;' postgres: 'ALTER TABLE "users" ADD FOREIGN KEY ("level_id") REFERENCES "level" ("id") ON DELETE CASCADE ON UPDATE CASCADE;'
}); });
}); });
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!