在belongsToMany关联中获得未知列的顺序

问题描述 投票:0回答:1

我有ChannelChannelUserConversation三种型号。我正在尝试从Channel模型中获取所有对话,但是遇到了一个未知的列问题((SequelizeDatabaseError:“字段列表”中的未知列'Conversations-> ChannelUser.ConversationId') ..>]

这些是我在模型中使用的关联,

频道模型

    static init(sequelize, DataTypes) {
        return super.init(
            {
                name: DataTypes.STRING(),
            },
            {
                sequelize,
                freezeTableName: true
            }
        );
    }

    static associate(models) {
        this.channelUser = this.hasMany(models.channelUser, { foreignKey: 'channelId' });
        this.conversation = this.belongsToMany(models.conversation, {
            through: {
                model: models.channelUser
            },
            foreignKey: 'channelId',
            otherKey: 'id'
        });
    }

ChannelUser Model

    static init(sequelize, DataTypes) {
        return super.init(
            {
            },
            {
                sequelize,
                freezeTableName: true
            }
        );
    }

    static associate(models) {
        this.channel = this.belongsTo(models.channel, { foreignKey: 'channelId' });
        this.user = this.belongsTo(models.user, { foreignKey: 'userId' });
        this.conversation = this.hasMany(models.conversation, {
            foreignKey: 'channelUserId',
        });
    }

会话模型

    static init(sequelize, DataTypes) {
        return super.init(
            {
                message: DataTypes.TEXT()
            },
            {
                sequelize,
                freezeTableName: true
            }
        );
    }

    static associate(models) {
        this.channelUser = this.belongsTo(models.channelUser, { foreignKey: 'channelUserId' });
    }

我也共享ER图以供参考,

ER diagram reference

这些是我参考的资源

  1. Sequelize Find belongsToMany Association
  2. Sequelize findAll with belongsToMany association
  3. How to disambiguate between multiple associations between the same models in Sequelize
  4. https://sequelize.org/master/class/lib/associations/belongs-to-many.js~BelongsToMany.html
  5. https://sequelize.readthedocs.io/en/v3/api/associations/belongs-to-many/

我有三个模型Channel,ChannelUser和Conversation。我正在尝试从Channel模型获取所有对话,但遇到了一个未知的列问题(SequelizeDatabaseError:Unknown column'...

node.js express sequelize.js associations
1个回答
0
投票

我认为,您在这里有一个糟糕的belongsToMany连接...假设很多对很多,您没有那个...通道用户表应该只连接通道和用户...您应该有一个多对多的用户对话连接表...并且一个频道本身只是对话中的hasMany,因为对话实际上只能属于一个频道。

© www.soinside.com 2019 - 2024. All rights reserved.