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

Commit 9ca86af2 by Austin deBruyn Committed by Sushant

fix(queryInterface/changeColumn): error when removing a column with unique constraint (#8386)

1 parent 3e0c9b1a
......@@ -342,6 +342,7 @@ const QueryGenerator = {
+ ' AND REFERENCED_COLUMN_NAME = ' + wrapSingleQuote(columnName)
+ ') OR (TABLE_NAME = ' + wrapSingleQuote(tableName)
+ ' AND COLUMN_NAME = ' + wrapSingleQuote(columnName)
+ ' AND REFERENCED_TABLE_NAME IS NOT NULL'
+ ')';
},
......
......@@ -689,6 +689,10 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
model: 'users',
key: 'id'
}
},
email: {
type: DataTypes.STRING,
unique: true
}
});
});
......@@ -730,6 +734,19 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
});
});
// From MSSQL documentation on ALTER COLUMN:
// The modified column cannot be any one of the following:
// - Used in a CHECK or UNIQUE constraint.
// https://docs.microsoft.com/en-us/sql/t-sql/statements/alter-table-transact-sql#arguments
if (dialect !== 'mssql') {
it('should be able to remove a column with unique contraint', function() {
return this.queryInterface.removeColumn('users', 'email').bind(this).then(function() {
return this.queryInterface.describeTable('users');
}).then(table => {
expect(table).to.not.have.property('email');
});
});
}
});
describe('(with a schema)', () => {
......@@ -750,6 +767,10 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
},
lastName: {
type: DataTypes.STRING
},
email: {
type: DataTypes.STRING,
unique: true
}
});
});
......@@ -798,6 +819,26 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
expect(table).to.not.have.property('id');
});
});
// From MSSQL documentation on ALTER COLUMN:
// The modified column cannot be any one of the following:
// - Used in a CHECK or UNIQUE constraint.
// https://docs.microsoft.com/en-us/sql/t-sql/statements/alter-table-transact-sql#arguments
if (dialect !== 'mssql') {
it('should be able to remove a column with unique contraint', function() {
return this.queryInterface.removeColumn({
tableName: 'users',
schema: 'archive'
}, 'email').bind(this).then(function() {
return this.queryInterface.describeTable({
tableName: 'users',
schema: 'archive'
});
}).then(table => {
expect(table).to.not.have.property('email');
});
});
}
});
});
......
......@@ -554,6 +554,12 @@ if (dialect === 'mysql') {
arguments: ['User', ['foo', 'bar']],
expectation: 'DROP INDEX `user_foo_bar` ON `User`'
}
],
getForeignKeyQuery: [
{
arguments: ['User', 'email'],
expectation: "SELECT CONSTRAINT_NAME as constraint_name FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE (REFERENCED_TABLE_NAME = 'User' AND REFERENCED_COLUMN_NAME = 'email') OR (TABLE_NAME = 'User' AND COLUMN_NAME = 'email' AND REFERENCED_TABLE_NAME IS NOT NULL)"
}
]
};
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!