在 Sequelize 迁移中创建关联 [英] Creating associations in Sequelize migration
问题描述
Node.js.续集 4.41.尝试通过另一个表制作具有多对多关系的 2 个模型.例如,使用 sequelize-cli 运行...
Nodejs. Sequelize 4.41. Try to make 2 models with relation many-to-many through another table. Running with sequelize-cli, for example...
sequelize model:generate --name Camera --attributes name:string,sn:string
这是模型
// Camera model
'use strict';
module.exports = (sequelize, DataTypes) => {
const Сamera = sequelize.define('Сamera', {
name: DataTypes.STRING,
sn: DataTypes.STRING
}, {});
Сamera.associate = function(models) {
// associations can be defined here
Camera.belongsToMany(models.Relay, {through: 'CameraRelay'});
};
return Сamera;
};
和
// Relay model
'use strict';
module.exports = (sequelize, DataTypes) => {
const Relay = sequelize.define('Relay', {
name: DataTypes.STRING,
sn: DataTypes.STRING,
channel: DataTypes.INTEGER
}, {});
Relay.associate = function(models) {
// associations can be defined here
Relay.belongsToMany(models.Camera, {through: 'CameraRelay'});
};
return Relay;
};
在文档中有短语
Belongs-To-Many 关联用于连接具有多个目标的源.此外,目标还可以连接到多个源.
Belongs-To-Many associations are used to connect sources with multiple targets. Furthermore the targets can also have connections to multiple sources.
Project.belongsToMany(User, {through: 'UserProject'});
User.belongsToMany(Project, {through: 'UserProject'});
这将使用等效的外键创建一个名为 UserProject 的新模型projectId 和 userId.
This will create a new model called UserProject with the equivalent foreign keys projectId and userId.
迁移是
// create-relay
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Relays', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING
},
sn: {
type: Sequelize.STRING
},
channel: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Relays');
}
};
和
//create camera
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Сameras', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING
},
sn: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Сameras');
}
};
为什么它在我运行 migrate 时不创建模型 CameraRelay 并且不为同一个模型创建迁移?
Why it doesn't create model CameraRelay and doesn't create migration for same model when I running migrate?
推荐答案
我想误解是关于 sync 与 migration:您所指的文档的很大一部分,正在使用 sync 方法从模型开始创建所有表和关联.
I guess the misunderstanding is about sync vs migration: great part of documentation you are referring to, is using the sync method to create all tables and associations starting from models.
当您使用迁移时,您正在使用迁移文件创建数据库所有表/列/关联(在我看来,这是用于生产的更好方法).
When you are using migrations, you are creating db all of your table/columns/associations using migration files (and in my hopinion, this is a better way for something that is going to production).
要了解差异,只需查看您的相机型号与相机迁移文件:
To understand the difference, just look at your camera model vs your camera migration file:
- 模型只定义了
name
和sn
属性 - 迁移文件当然有
name
和sn
,但它有id
、createdAt
和>updatedAt
也是.
- the model has only
name
andsn
properties defined - the migration file has of course
name
andsn
, but it hasid
,createdAt
andupdatedAt
too.
迁移文件旨在以安全的方式更改您的数据库,允许您回滚到过去的任何时间点.
Migrations are file with the aim of change your db in a safe way, allowing you to rollback to any point in the past.
所以,回到你的问题,你必须:
So, back to your problem, you have to:
- 创建一个新的迁移文件来创建新的 CameraRelay 表,其中包含 Camera 和 Relay 表的外键
- 使用与 CameraRelay 表的一对多关系更新您当前的相机迁移文件
- 使用与 CameraRelay 表的一对多关系更新您当前的 Relay 迁移文件
CameraRelay 迁移示例:
CameraRelay migration example:
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('CameraRelays', {
cameraId: {
primaryKey: true,
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'Relay',
key: 'id'
}
},
relayId: {
primaryKey: true,
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'Camera',
key: 'id'
}
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('CameraRelays');
}
};
这篇关于在 Sequelize 迁移中创建关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!