简体   繁体   English

使用Sequelize CLI进行MySql迁移。 无法添加外键约束

[英]MySql migrations with Sequelize CLI. Cannot add foreign key constraint

I want to migrate this column: 我想迁移此列:

countryId: {
   type: Sequelize.DataTypes.CHAR(2),
   allowNull: true,
   references: {
    model: 'country',
    key: 'country_code'
   },
   field: 'country_id'
},

country_code: 国家代码:

countryCode: {
  type: DataTypes.CHAR(2),
  allowNull: false,
  primaryKey: true,
  field: 'country_code'
},

Full console.log: 完整的console.log:

    CREATE TABLE IF NOT EXISTS `address` (`address_id` BIGINT NOT NULL
 auto_increment , `state_id` INTEGER(11) UNIQUE, `country_id` CHAR(2), 
 PRIMARY KEY (`address_id`), FOREIGN KEY (`state_id`) REFERENCES 
`province` (`province_id`), FOREIGN KEY (`country_id`) REFERENCES 
`country` (`country_code`)) ENGINE=InnoDB;

But I get 但是我明白了

ERROR: Cannot add foreign key constraint 错误:无法添加外键约束

When I add unique: true to country_id , it still doesn't work for some reason. 当我在country_id添加unique: true时,由于某种原因它仍然无法正常工作。

我认为您必须使用此处描述的命令手动设置关联: http : //docs.sequelizejs.com/manual/tutorial/associations.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM