var state = require('./state')
module.exports = function (sequelize, DataTypes) {
var city = sequelize.define('city', {
city_id : {
type : DataTypes.INTEGER,
autoIncrement : true,
primaryKey : true
},
city_name : {
type : DataTypes.STRING,
allowNull : false,
unique : true
},
city_state_id : {
type : DataTypes.INTEGER,
allowNull : false,
references : {
model : 'states',
key : 'state_id'
}
}
}
return city;
}
Это мой файл ссылок.Unhandled Rejection SequelizeDatabaseError: ошибка ограничения внешнего ключа: не может добавить внешний ключ
module.exports = function (sequelize, DataTypes) {
var state = sequelize.define('state', {
state_id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
state_name: {
type: DataTypes.STRING,
allowNull: false,
unique: true
},
})
return state;
}
Я не знаю, почему я не могу добавить ограничение внешнего ключа здесь. Кто-нибудь может мне помочь? Я получаю сообщение об ошибке, как это:
Unhandled отказ SequelizeDatabaseError: ER_CANNOT_ADD_FOREIGN: Невозможно добавить ограничение внешнего ключа Когда я пытаюсь выполнить migration.js на терминал: узел migration.js
migration.js:
/*jslint node: true */
'use strict';
var Sequelize = require('sequelize');
var fs = require('fs');
var schema_dir = './schema/';
var config = require('./config').mysql_conf;
var connection = new Sequelize(config.database, config.username, config.password, config);
fs.readdir(schema_dir, function(err, files) {
if (err) {
console.log(err);
} else {
files = files.sort();
console.log(files);
for (var i = 0; i < files.length; i++) {
var module_name = files[i].slice(0, -3);
console.log(schema_dir + files[i]);
// var schema = require(schema_dir + module_name);
try {
var model = connection['import']('' + schema_dir + module_name);
model.sync().then(function() {
console.log("table created: " + module_name);
});
model.sync({force : true})
} catch (ex) {
console.log("error with module_name: " + module_name, ex);
}
}
}
});
У кого-нибудь есть идея, почему это происходит?