首页 > 解决方案 > 为什么sequelize会自动创建两个表?

问题描述

当我使用 sequelize.sync() 部署表时。它创建两个表( tagable , tagables )。我已经明确定义了表名。但续集创建两个表。如果我将 tableName 设置为单数(可标记),它只会创建 1 个表。

class Tagable extends Model {}

Tagable.init({
    id: {
        type: DataTypes.BIGINT.UNSIGNED,
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        unique: true
    },
    tag_id: {
        type: DataTypes.MEDIUMINT.UNSIGNED,
        allowNull: false,
        references: {
            model: 'tags',
            key: 'id'
        }
    },
    tagable_id: {
        type: DataTypes.BIGINT.UNSIGNED,
        allowNull: false
    },
    tagable_type: {
        type: DataTypes.TINYINT.UNSIGNED,
        allowNull: false
    }
}, {
    sequelize,
    modelName: 'Tagable',
    tableName: 'tagables',
    engine: 'MYISAM',
    charset: 'utf8mb4',
    collate: 'utf8mb4_unicode_ci',
    timestamps: false 
});

标签: node.jssequelize.js

解决方案


推荐阅读