尝试创建一个非常简单的概念验证来创建与迁移的表关联,但在通过cli运行时无法添加外键 - ./node_modules/.bin/sequelize db:migrate
确保清除我的SequelizeMeta& SequelizeData表并在运行之前重置所有内容,但仍然会收到错误。
当我删除userId
create-company.js
字段定义时,迁移运行正常
创建-user.js的
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('users', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
firstName: {
type: Sequelize.STRING,
},
lastName: {
type: Sequelize.STRING,
},
email: {
type: Sequelize.STRING,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
},
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('users');
},
};
创建-company.js
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('companies', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
name: {
type: Sequelize.STRING,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
},
userId: {
type: Sequelize.UUID, // have also tried Sequelize.INTEGER
references: {
model: 'users',
key: 'id',
},
},
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('companies');
},
};
在我的companies
表
获取以下输出:
Sequelize CLI [Node: 9.7.1, CLI: 4.0.0, ORM: 4.36.0]
Loaded configuration file "src/database/config.js".
Using environment "dev".
== 20180309004031-create-user: migrating =======
== 20180309004031-create-user: migrated (0.036s)
== 20180316173751-create-company: migrating =======
ERROR: Cannot add foreign key constraint
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! setup1@1.0.0 migrate:dev: `NODE_ENV=dev ./node_modules/.bin/sequelize db:migrate`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the setup1@1.0.0 migrate:dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/shoma/.npm/_logs/2018-03-16T18_22_27_398Z-debug.log
方言: mysql innoDB utf8 方言版本: 5.7.21 Homebrew 数据库版本: 5.7.21 Homebrew Sequelize版本: Sequelize 4.35.2, Sequelize CLI版本: Sequelize-cli 4.0.0 使用最新版本测试:否(如果是,请指定该版本)
答案 0 :(得分:3)
引用字段必须是相同类型。但是你有“company.userId:UUID”和“user.id:INTEGER”。