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

Commit d37b0ce0 by ming.deng

fix `addColumn` with reference not add reference in mysql

1 parent 55da7651
# Future
- [FIXED] `addColumn` with reference in mysql [#5592](https://github.com/sequelize/sequelize/issues/5592)
- [ADDED] rejectOnEmpty mode [#272](https://github.com/sequelize/sequelize/issues/272) [#5480](https://github.com/sequelize/sequelize/issues/5480)
- [ADDED] `beforeCount` hook [#5209](https://github.com/sequelize/sequelize/pull/5209)
- [ADDED] `validationFailed` hook [#1626](https://github.com/sequelize/sequelize/issues/1626)
......
......@@ -102,7 +102,8 @@ var QueryGenerator = {
, attribute = Utils._.template('<%= key %> <%= definition %>')({
key: this.quoteIdentifier(key),
definition: this.attributeToSQL(dataType, {
context: 'addColumn'
context: 'addColumn',
foreignKey: key
})
});
......@@ -229,7 +230,7 @@ var QueryGenerator = {
return Utils._.template(sql)({ tableName: this.quoteIdentifiers(tableName), indexName: indexName });
},
attributeToSQL: function(attribute) {
attributeToSQL: function(attribute, options) {
if (!Utils._.isPlainObject(attribute)) {
attribute = {
type: attribute
......@@ -265,6 +266,16 @@ var QueryGenerator = {
if (attribute.references) {
attribute = Utils.formatReferences(attribute);
if (options && options.context === 'addColumn' && options.foreignKey) {
var attrName = options.foreignKey;
template += Utils._.template(', ADD CONSTRAINT <%= fkName %> FOREIGN KEY (<%= attrName %>)')({
fkName: this.quoteIdentifier(attrName + '_foreign_idx'),
attrName: this.quoteIdentifier(attrName)
});
}
template += ' REFERENCES ' + this.quoteTable(attribute.references.model);
if (attribute.references.key) {
......
'use strict';
/* jshint -W030, -W110 */
var Support = require(__dirname + '/../support')
, DataTypes = require('../../../lib/data-types')
, expectsql = Support.expectsql
, current = Support.sequelize
, sql = current.dialect.QueryGenerator;
if (current.dialect.name === 'mysql') {
describe(Support.getTestDialectTeaser('SQL'), function() {
describe('addColumn', function () {
var Model = current.define('users', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
}
}, { timestamps: false });
it('properly generate alter queries', function(){
return expectsql(sql.addColumnQuery(Model.getTableName(), 'level_id', current.normalizeAttribute({
type: DataTypes.FLOAT,
allowNull: false,
})), {
mysql: 'ALTER TABLE `users` ADD `level_id` FLOAT NOT NULL;',
});
});
it('properly generate alter queries for foreign keys', function(){
return expectsql(sql.addColumnQuery(Model.getTableName(), 'level_id', current.normalizeAttribute({
type: DataTypes.INTEGER,
references: {
model: 'level',
key: 'id'
},
onUpdate: 'cascade',
onDelete: 'cascade'
})), {
mysql: 'ALTER TABLE `users` ADD `level_id` INTEGER, ADD CONSTRAINT `level_id_foreign_idx` 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!