oms/backend/database/migrations/20260909000102-create-principal-users.js

56 lines
1.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/** 3. 主体用户关联表:一个用户可属于多个主体,一个主体可有多个用户 */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable(
'principal_users',
{
id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
autoIncrement: true,
primaryKey: true,
},
principal_id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
comment: '主体ID',
references: { model: 'principals', key: 'id' },
},
user_id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
comment: '用户ID',
references: { model: 'users', key: 'id' },
},
role: { type: Sequelize.STRING(50), allowNull: true, comment: '在该主体下的角色标识' },
is_default: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 0, comment: '是否默认主体:1是 0否' },
status: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 1, comment: '状态:1启用 0禁用' },
created_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
},
updated_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
},
},
{ charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci', comment: '主体用户关联表' }
);
await queryInterface.addIndex('principal_users', {
fields: [ 'principal_id', 'user_id' ],
unique: true,
name: 'uk_principal_users',
});
await queryInterface.addIndex('principal_users', { fields: [ 'user_id' ], name: 'idx_principal_users_user' });
},
async down(queryInterface) {
await queryInterface.dropTable('principal_users');
},
};