【发布时间】:2021-10-08 23:50:42
【问题描述】:
我有两个具有多对多关系的表,角色表和权限表,我需要为我的表roles_permission 播种,所有初始权限都设置在权限播种器中,我在 sequelize 的文档中搜索了如何执行此操作但我还没有找到其他可以帮助我的方法
我的角色种子
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.bulkInsert('roles', [{
name: 'SUPER_ROLE',
created_at: new Date(),
updated_at: new Date()
}],{});
},
down: async (queryInterface, Sequelize) => {
return queryInterface.bulkDelete('roles', null, {});
}
};
我的权限种子
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
return queryInterface.bulkInsert('permissions', [{
name: 'create_products',
created_at: new Date(),
updated_at: new Date()
}, {
name: 'edit_products',
created_at: new Date(),
updated_at: new Date()
}, {
name: 'delete_products',
created_at: new Date(),
updated_at: new Date()
}, {
name: 'view_products',
created_at: new Date(),
updated_at: new Date()
}],
{});
},
down: async (queryInterface, Sequelize) => {
return await queryInterface.bulkDelete('permissions', null, {});
}
};
我的数据透视表迁移
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
return queryInterface.createTable('roles_permissions', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false
},
role_id: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'roles', key: 'id'
},
onUpdate: 'CASCADE',
onDelete: 'CASCADE'
},
permission_id: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'permissions', key: 'id'
},
onUpdate: 'CASCADE',
onDelete: 'CASCADE'
},
created_at: {
type: Sequelize.DATE,
allowNull: false,
},
updated_at: {
type: Sequelize.DATE,
allowNull: false,
},
})
},
down: async (queryInterface, Sequelize) => {
return queryInterface.dropTable('roles_permissions');
}
};
【问题讨论】:
标签: node.js sequelize.js sequelize-cli