Sequelize 拥有并属于许多

Sequelize has and belongs to many

有没有办法通过 table 进行多态自关联(例如,Collection 拥有并属于许多 Collections)?

尝试使 http://docs.sequelizejs.com/manual/tutorial/associations.html#n-m 适应这种情况:

// Inside collection.js associate
Collection.belongsToMany(Collection, {
    through: {
        model: models.CollectionItem,
        unique: false,
        scope: {
            collectible: 'collection'
        }
    },
    foreignKey: 'collectibleUid',
    constraints: false
});

collectionItem.js 看起来像

const CollectionItem = sequelize.define("CollectionItem", {
    uid: {
        type: DataTypes.BIGINT,
        primaryKey: true
    },
    collectionUid: {
        type: DataTypes.BIGINT,
        allowNull: false,
        unique: 'collection_item_collectible'
    },
    order: {
        type: DataTypes.INTEGER,
        allowNull: false,
        defaultValue: 0
    },
    collectibleUid: {
        type: DataTypes.BIGINT,
        allowNull: false,
        references: null, // Because the column is polymorphic, we cannot say that it REFERENCES a specific table
        unique: 'collection_item_collectible'
    },
    collectible: {
        type: DataTypes.STRING,
        allowNull: false,
        unique: 'collection_item_collectible'
    }
}, {
    classMethods: {
    }
});

似乎 Sequelize 希望我通过另一个连接/通过 table 以不同的方式命名它,但这本质上是创建一个新的 table 而不是仅仅获得一个真正的 hasAndBelongsToMany 类型关系。

Error: 'as' must be defined for many-to-many self-associations

尝试明确并添加:

as: "CollectionItem"

as 不会创建新连接 table,它只是给关系命名