Node.js 如何使用Sequelize在迁移中添加对另一个表列的引用作为foreignKey
在我的节点API中,我正在使用ORM Sekuilize,并试图理解如何使迁移文件以这种方式通过外键相互引用到表中 我有一个学生,他有许多项目和属于一个学生的项目 所以我的模型是这样的:Node.js 如何使用Sequelize在迁移中添加对另一个表列的引用作为foreignKey,node.js,postgresql,sequelize.js,Node.js,Postgresql,Sequelize.js,在我的节点API中,我正在使用ORM Sekuilize,并试图理解如何使迁移文件以这种方式通过外键相互引用到表中 我有一个学生,他有许多项目和属于一个学生的项目 所以我的模型是这样的: module.exports = (sequelize, DataTypes) => { const Student = sequelize.define("Student", { _id: { primaryKey: true, t
module.exports = (sequelize, DataTypes) => {
const Student = sequelize.define("Student", {
_id: {
primaryKey: true,
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
//autoIncrement: true
},
name: {
type: DataTypes.STRING,
allowNull: false
},
email: {
type: DataTypes.TEXT,
allowNull: false,
unique: true
},
dateOfBirth: {
type: DataTypes.DATEONLY,
allowNull: false
},
createdAt: {
type: DataTypes.DATE,
allowNull: false
},
updatedAt: {
type: DataTypes.DATE,
allowNull: false
}
});
Student.associate = function(models) {
Student.hasMany(models.Project, {
foreignKey: 'student_id',
as: 'projects',
onDelete: 'CASCADE',
});
};
return Student;
};
项目模型:
module.exports = (sequelize, DataTypes) => {
const Project = sequelize.define("Project", {
project_id: {
primaryKey: true,
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
//autoIncrement: true
},
student_id: {
type: DataTypes.UUIDV4,
allowNull: false,
},
name: {
type: DataTypes.STRING,
allowNull: false,
unique: true
},
description: {
type: DataTypes.TEXT,
allowNull: true
},
creationDate: {
type: DataTypes.DATEONLY,
allowNull: true
},
repoUrl: {
type: DataTypes.TEXT,
allowNull: true
},
liveUrl: {
type: DataTypes.TEXT,
allowNull: true
},
createdAt: {
type: DataTypes.DATE,
allowNull: false
},
updatedAt: {
type: DataTypes.DATE,
allowNull: false
}
});
Student.associate = function(models) {
Project.belongsTo(models.Student, {
foreignKey: "student_id",
as: "student",
onDelete: "CASCADE"
});
};
return Student;
};
我的迁移如下所示,但正如我之前所述,我不知道如何包含外键,而且我的工作方式也不可行,迁移是创建的,但在DB中没有建立关系:
学生迁移
"use strict";
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable("Students", {
_id: {
primaryKey: true,
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
//autoIncrement: true
},
name: {
type: Sequelize.STRING,
allowNull: false
},
email: {
type: Sequelize.TEXT,
allowNull: false,
unique: true
},
dateOfBirth: {
type: Sequelize.DATEONLY,
allowNull: false
},
createdAt: {
type: Sequelize.DATE,
allowNull: false
},
updatedAt: {
type: Sequelize.DATE,
allowNull: false
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable("Students");
}
};
"use strict";
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable("Projects", {
project_id: {
primaryKey: true,
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4
//autoIncrement: true
},
student_id: {
foreignKey: true,
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
allowNull: false
},
name: {
type: Sequelize.STRING,
allowNull: false,
unique: true
},
description: {
type: Sequelize.TEXT,
allowNull: true
},
creationDate: {
type: Sequelize.DATEONLY,
allowNull: true
},
repoUrl: {
type: Sequelize.TEXT,
allowNull: true
},
liveUrl: {
type: Sequelize.TEXT,
allowNull: true
},
createdAt: {
type: Sequelize.DATE,
allowNull: false
},
updatedAt: {
type: Sequelize.DATE,
allowNull: false
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable("Projects");
}
};
项目迁移
"use strict";
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable("Students", {
_id: {
primaryKey: true,
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
//autoIncrement: true
},
name: {
type: Sequelize.STRING,
allowNull: false
},
email: {
type: Sequelize.TEXT,
allowNull: false,
unique: true
},
dateOfBirth: {
type: Sequelize.DATEONLY,
allowNull: false
},
createdAt: {
type: Sequelize.DATE,
allowNull: false
},
updatedAt: {
type: Sequelize.DATE,
allowNull: false
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable("Students");
}
};
"use strict";
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable("Projects", {
project_id: {
primaryKey: true,
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4
//autoIncrement: true
},
student_id: {
foreignKey: true,
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
allowNull: false
},
name: {
type: Sequelize.STRING,
allowNull: false,
unique: true
},
description: {
type: Sequelize.TEXT,
allowNull: true
},
creationDate: {
type: Sequelize.DATEONLY,
allowNull: true
},
repoUrl: {
type: Sequelize.TEXT,
allowNull: true
},
liveUrl: {
type: Sequelize.TEXT,
allowNull: true
},
createdAt: {
type: Sequelize.DATE,
allowNull: false
},
updatedAt: {
type: Sequelize.DATE,
allowNull: false
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable("Projects");
}
};
以下是迁移工作流:
sequelize cli
生成模型和迁移文件李>
学生
和项目
之间的关联student\u id
列添加到Project
表)。对于您的情况,Project
表有一个student\u id
列作为其对student
表的\u id
列的外键引用models/student.js
:
module.exports=(续集,数据类型)=>{
const Student=sequelize.define('Student'{
_身份证:{
primaryKey:没错,
类型:DataTypes.UUID,
defaultValue:DataTypes.UUIDV4,
},
姓名:{
类型:DataTypes.STRING,
allowNull:错,
},
电邮:{
类型:DataTypes.TEXT,
allowNull:错,
独一无二:没错,
},
出生日期:{
类型:DataTypes.DATEONLY,
allowNull:错,
},
创建数据:{
类型:DataTypes.DATE,
allowNull:错,
},
更新日期:{
类型:DataTypes.DATE,
allowNull:错,
},
});
Student.associate=函数(模型){
学生。有许多(模型。项目{
外键:“学生id”,
例如:“项目”,
});
};
留学生;
};
models/project.js
:
module.exports=(续集,数据类型)=>{
const Project=sequelize.define('Project'{
项目编号:{
primaryKey:没错,
类型:DataTypes.UUID,
defaultValue:DataTypes.UUIDV4,
},
姓名:{
类型:DataTypes.STRING,
allowNull:错,
独一无二:没错,
},
说明:{
类型:DataTypes.TEXT,
阿洛诺:是的,
},
创作日期:{
类型:DataTypes.DATEONLY,
阿洛诺:是的,
},
报告网址:{
类型:DataTypes.TEXT,
阿洛诺:是的,
},
liveUrl:{
类型:DataTypes.TEXT,
阿洛诺:是的,
},
创建数据:{
类型:DataTypes.DATE,
allowNull:错,
},
更新日期:{
类型:DataTypes.DATE,
allowNull:错,
},
});
Project.associate=功能(模型){
Project.belongsTo(models.Student{
外键:“学生id”,
作为‘学生’,
onDelete:'级联',
});
};
返回项目;
};
migrations/20200213113039创建student.js
:
“严格使用”;
module.exports={
up:(查询接口,续集)=>{
返回queryInterface.createTable('Students'{
_身份证:{
primaryKey:没错,
类型:Sequelize.UUID,
defaultValue:Sequelize.UUIDV4,
},
姓名:{
类型:Sequelize.STRING,
allowNull:错,
},
电邮:{
类型:Sequelize.TEXT,
allowNull:错,
独一无二:没错,
},
出生日期:{
类型:Sequelize.DATEONLY,
allowNull:错,
},
创建数据:{
类型:Sequelize.DATE,
allowNull:错,
},
更新日期:{
类型:Sequelize.DATE,
allowNull:错,
},
});
},
向下:(查询接口,续集)=>{
返回queryInterface.dropTable('Students');
},
};
migrations/202002113231创建project.js
:
“严格使用”;
module.exports={
up:(查询接口,续集)=>{
返回queryInterface.createTable('Projects'{
项目编号:{
primaryKey:没错,
类型:Sequelize.UUID,
defaultValue:Sequelize.UUIDV4,
},
学生证:{
类型:Sequelize.UUID,
参考资料:{
型号:{
表名:'学生',
},
键:“\u id”,
},
allowNull:错,
onDelete:'级联',
},
姓名:{
类型:Sequelize.STRING,
allowNull:错,
独一无二:没错,
},
说明:{
类型:Sequelize.TEXT,
阿洛诺:是的,
},
创作日期:{
类型:Sequelize.DATEONLY,
阿洛诺:是的,
},
报告网址:{
类型:Sequelize.TEXT,
阿洛诺:是的,
},
liveUrl:{
类型:Sequelize.TEXT,
阿洛诺:是的,
},
创建数据:{
类型:Sequelize.DATE,
allowNull:错,
},
更新日期:{
类型:Sequelize.DATE,
allowNull:错,
},
});
},
向下:(查询接口,续集)=>{
返回queryInterface.dropTable('Projects');
},
};
执行命令npx sequelize cli db:migrate后,检查数据库中的表信息(我还创建了种子文件以插入一些演示数据):
node sequelize examples=#从“学生”中选择*;
_id |姓名|电子邮件|出生日期|创建日期|更新日期
--------------------------------------+---------------+--------------------------+-------------+-------------------------------+-------------------------------
2c6db3cb-82c7-4728-a259-23520bd760d0 |罗莎琳德·莫尔|娜塔莎。Littel@gmail.com | 2020-02-13 | 2020-02-13 12:39:23.508379+00 | 2020-02-13 12:39:23.508379+00
(1排)
节点续集示例