node.js - 续集 addConstraint 语法
问题描述
我试图添加两个表,播放列表和标签。标签可选地有一个播放列表,许多播放列表可以与同一个标签相关。
为了将外键约束添加到播放列表,我尝试使用 addConstraint 添加它。文档对此很糟糕。
这就是我所拥有的:
"use strict";
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable("Playlist", {
id: { type: Sequelize.STRING, primaryKey: true, allowNull: false },
track_uri: Sequelize.STRING,
track: Sequelize.STRING(150),
artist: Sequelize.STRING(150),
added_at: {
type: Sequelize.DATE,
defaultValue: new Date(),
allowNull: false,
},
created_at: {
type: Sequelize.DATE,
defaultValue: new Date(),
allowNull: false,
},
is_album_track: Sequelize.BOOLEAN,
label: { type: Sequelize.STRING, unique: true },
});
await queryInterface.createTable("Label", {
id: { type: Sequelize.STRING, primaryKey: true },
playlist_link: {
type: Sequelize.STRING(150),
unique: true,
allowNull: false,
},
password: { type: Sequelize.STRING, allowNull: false },
email: { type: Sequelize.STRING, allowNull: false },
playlist: {
type: Sequelize.STRING,
foreignKey: true,
references: {
model: "Playlist",
label: "id",
},
},
});
await queryInterface.addConstraint("Playlist", {
type: "foreign key",
fields: ["label"],
name: "label_fkey",
references: {
table: "Label",
field: "id",
},
onDelete: "cascade",
onUpdate: "cascade",
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable("Label");
await queryInterface.dropTable("Playlist");
},
};
我收到此错误:
ERROR: column "label" referenced in foreign key constraint does not exist
解决方案
推荐阅读
- python - 在 PySimpleGUI 中更新值
- python - Django-rest-framework :创建序列化程序,以使用一个自定义用户模型注册两种不同的用户类型外键关系
- android-studio - 将图像保存在图库中时如何修复错误?
- scala - 如何避免使用 Scala 类型为通用 Spark RDD 重载方法?
- javascript - 如何使用单个事件侦听器而不是每个按钮中的 onclick 事件来做一个迷你计算器?
- vue.js - 找不到模块'@/assets/
.svg'。@vue/cli 版本:4.2.3 和 4.3.1 - sql - 在表中查找 SQL Server 中的非空列
- java - 在 Java 中使用锚标记链接到外部 URL
- c - 为什么带有 \n 的 printf() 仍然无法在 Windows 上刷新?
- python-3.x - Tensorflow 2.1 TPU v2 使用 bfloat16 减少内存使用