gpt4 book ai didi

node.js - Sequelize 在belongsToMany 关联中获取未知列

转载 作者:行者123 更新时间:2023-12-03 22:25:57 25 4
gpt4 key购买 nike

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

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

channel 模型

    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'
});
}

channel 用户模型

    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

这些是我引用的来源
  • Sequelize Find belongsToMany Association
  • Sequelize findAll with belongsToMany association
  • How to disambiguate between multiple associations between the same models in Sequelize
  • https://sequelize.org/master/class/lib/associations/belongs-to-many.js~BelongsToMany.html
  • https://sequelize.readthedocs.io/en/v3/api/associations/belongs-to-many/
  • 最佳答案

    好吧,我的看法是,您在这里加入了一个不好的belongsToMany……假设是多对多,而您没有…… channel 用户表应该只加入 channel 和用户……您应该有一个多对多的用户对话连接表......并且一个 channel 本身就是一个 hasMany 对话,因为一个对话实际上只能属于一个 channel 。

    关于node.js - Sequelize 在belongsToMany 关联中获取未知列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60675122/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com