gpt4 book ai didi

sequelize.js - Sequelize 运行迁移无法添加外键约束

转载 作者:行者123 更新时间:2023-12-03 22:32:58 25 4
gpt4 key购买 nike

我创建了两个模型。在我想为他们创建迁移之后。
配置模型。该配置将“hasMany”与域相关联。一个配置可以与多个域相关。

const { Model } = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Config extends Model {
static associate(models) {
Config.hasMany(models.domain, {
foreignKey: 'configId',
onDelete: 'CASCADE',
});
}
}

Config.init(
{
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER,
},
country: {
type: DataTypes.STRING,
allowNull: false,
},
},
{
sequelize,
tableName: 'configs',
modelName: 'config',
timestamps: false,
},
);
return Config;
};
配置模型。该域具有与配置相关的“belongsTo”。一个域可以与一个配置相关。
const { Model } = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Domain extends Model {
static associate(models) {
Domain.belongsTo(models.config, {
foreignKey: 'configId',
});
}
}

Domain.init(
{
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER,
},
domain: {
type: DataTypes.STRING,
allowNull: false,
},
isActive: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
},
{
sequelize,
tableName: 'domains',
modelName: 'domain',
timestamps: false,
},
);
return Domain;
};
在我创建了两个迁移之后。
配置迁移。
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('configs', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
country: {
type: Sequelize.STRING,
allowNull: false,
},
});
},

down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('configs');
},
};
域迁移
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('domains', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
domain: {
type: Sequelize.STRING,
allowNull: false,
},
isActive: {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: false,
},
configId: {
type: Sequelize.INTEGER,
onDelete: 'CASCADE',
references: {
model: 'configs',
key: 'id',
},
},
});
},

down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('domains');
},
};
然后我运行我的迁移并出现错误。
user@user-ps:~/Documents/projects/app-api$ npm run migration:run

> app-api@1.0.0 migration:run
> sequelize db:migrate


Sequelize CLI [Node: 15.3.0, CLI: 6.2.0, ORM: 6.3.5]

Loaded configuration file "src/config/config.js".
== 20201130161539-affiliate: migrating =======

ERROR: Cannot add foreign key constraint

npm ERR! code 1
npm ERR! path /home/user/Documents/projects/app-api
npm ERR! command failed
npm ERR! command sh -c sequelize db:migrate

npm ERR! A complete log of this run can be found in:
npm ERR! /home/user/.npm/_logs/2020-12-01T16_51_54_422Z-debug.log
我哪里做错了?我想创建迁移然后我可以运行或反转。
日志
Loaded configuration file "src/config/config.js".
Executing (default): SELECT 1+1 AS result
Executing (default): SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA = 'mydb';
Executing (default): SHOW FULL COLUMNS FROM `SequelizeMeta`;
Executing (default): CREATE TABLE IF NOT EXISTS `SequelizeMeta` (`name` VARCHAR(255) NOT NULL UNIQUE , PRIMARY KEY (`name`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_unicode_ci;
Executing (default): SHOW INDEX FROM `SequelizeMeta`
Executing (default): SELECT `name` FROM `SequelizeMeta` AS `SequelizeMeta` ORDER BY `SequelizeMeta`.`name` ASC;
Executing (default): CREATE TABLE IF NOT EXISTS `SequelizeMeta` (`name` VARCHAR(255) NOT NULL UNIQUE , PRIMARY KEY (`name`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_unicode_ci;
Executing (default): SHOW INDEX FROM `SequelizeMeta`
Executing (default): SELECT `name` FROM `SequelizeMeta` AS `SequelizeMeta` ORDER BY `SequelizeMeta`.`name` ASC;
Executing (default): CREATE TABLE IF NOT EXISTS `SequelizeMeta` (`name` VARCHAR(255) NOT NULL UNIQUE , PRIMARY KEY (`name`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_unicode_ci;
Executing (default): SHOW INDEX FROM `SequelizeMeta`
Executing (default): SELECT `name` FROM `SequelizeMeta` AS `SequelizeMeta` ORDER BY `SequelizeMeta`.`name` ASC;
== 20201130161606-domain: migrating =======
Executing (default): CREATE TABLE IF NOT EXISTS `domains` (`id` INTEGER NOT NULL auto_increment , `domain` VARCHAR(255) NOT NULL, `isActive` TINYINT(1) NOT NULL DEFAULT false, `configId` INTEGER, PRIMARY KEY (`id`), FOREIGN KEY (`configId`) REFERENCES `configs` (`id`) ON DELETE CASCADE) ENGINE=InnoDB;

最佳答案

“迁移”路径中的JS文件顺序问题:
在生成“域”表之前,生成“配置”表。

关于sequelize.js - Sequelize 运行迁移无法添加外键约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65097616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com