【问题标题】:In Nodejs I keep getting Sequelize.js - “is not associated to” error在 Nodejs 中,我不断收到 Sequelize.js - “未关联”错误
【发布时间】:2020-01-17 08:55:32
【问题描述】:

我在 node.js 中有以下 2 个表:

用户

'use strict'
let Sequelize = require('sequelize');
let dbConnection = require('../connection.js');
let users = dbConnection.connection.define('users', {
        //user table detail
}, {
    classMethods: {associate: function (models) {
        // associations can be defined here
        users.hasMany(models.chat_histories, {foreignKey: 'sender_id'});
        users.hasMany(models.chat_histories, {foreignKey: 'receiver_id'});
    }}
}, {
    timestamps: false
});

module.exports = users;

chat_histories

'use strict'
let Sequelize = require('sequelize');
let dbConnection = require('../connection.js');
let chat_histories = dbConnection.connection.define(
    'chat_histories', {
        //chat_history detail
        link_id: {
            type: Sequelize.STRING(64),
            references: 'links', // <<< Note, its table's name, not object name
            referencesKey: 'id' // <<< Note, its a column name
        },
        sender_id: {
            type: Sequelize.BIGINT,
            references: 'users', // <<< Note, its table's name, not object name
            referencesKey: 'id' // <<< Note, its a column name
        },
        receiver_id: {
            type: Sequelize.BIGINT,
            references: 'users', // <<< Note, its table's name, not object name
            referencesKey: 'id' // <<< Note, its a column name
        }
    },
    {
        classMethods: {associate: function (models) {
            // associations can be defined here
            chat_histories.belongsTo(models.users, {foreignKey: 'sender_id'});
            chat_histories.belongsTo(models.users, {foreignKey: 'receiver_id'});
            chat_histories.belongsTo(models.links, {foreignKey: 'link_id' });
        }}
    }, {
        timestamps: false
    }
);

module.exports = chat_histories;

但是,每次在 chat_history 表中查询时,我都会收到以下错误消息:

SequelizeEagerLoadingError:chat_histories 未与用户关联!

有什么问题吗?

通过在 chat_histories.js 中添加以下内容,我可以看到自己已经将 chat_historeis 与 users 表相关联

classMethods: {associate: function (models) {
    // associations can be defined here
    chat_histories.belongsTo(models.users, {foreignKey: 'sender_id'});
    chat_histories.belongsTo(models.users, {foreignKey: 'receiver_id'});
    chat_histories.belongsTo(models.links, {foreignKey: 'link_id' });
}}

并在user.js中添加以下内容

classMethods: {associate: function (models) {
    // associations can be defined here
    users.hasMany(models.chat_histories, {foreignKey: 'sender_id'});
    users.hasMany(models.chat_histories, {foreignKey: 'receiver_id'});
    users.hasMany(models.links, {foreignKey: 'owner_id'});
}}

下面是我如何搜索 SQL 语句:

从用户中选择不同的用户,chat_histories where (users.id = chat_histories.sender_id 或 users.id = chat_histories.receiver_id)和 chat_histories.link_id = :link_id;

let chat_histories = model.chat_histories;
let users = model.users;
let usersList;
const Op = Sequelize.Op;
await users.findAll({
    where: {
        [Op.and]: [
            {
                [Op.or]:[
                    {id: chat_histories.sender_id}, 
                    {id: chat_histories.receiver_id}
                ]
            }, 
            {
                link_id: link_id
            }
        ],
    },
    include: [
        {
            model: chat_histories, 
            required: false
        },
    ]
}).then(function(data) {
        usersList = data;
});

【问题讨论】:

    标签: node.js sequelize.js


    【解决方案1】:

    您没有正确定义关联。需要为userschat_histories 表创建关联。

    users.hasMany(models.chat_histories, { foreignKey: 'sender_id', as: 'chat_histories' });
    chat_histories.belongsTo(models.users, { foreignKey: 'sender_id', as: 'user' });
    

    我从未在 Sequelize 模型中使用像 referencesreferencesKey 这样的关键字来定义关联。我在文档中都没有找到任何关于它们的参考。

    希望对你有帮助!

    【讨论】:

    • 感谢您的回复!我尝试添加: users.hasMany(models.chat_histories, { foreignKey: 'sender_id', as: 'chat_histories' }); users.hasMany(models.chat_histories, { foreignKey: 'receiver_id', as: 'chat_histories' }); chat_histories.belongsTo(models.users, { foreignKey: 'sender_id', as: 'user' }); chat_histories.belongsTo(models.users, { foreignKey: 'receiver_id', as: 'user' });到 users.js 和 chat_histiroes.js 但仍然得到同样的错误
    • 我请求您检查 sequelize 在如上所述指定关联后生成的查询。要获取查询,您可以使用 sequelize sync。更多详情请参考以下问题stackoverflow.com/questions/21066755/…
    • 我更新了我的原始帖子,提供了有关该课程的更多详细信息,还包括了两个课程的关联。请看一看。谢谢。
    • 要了解如何定义续集模型之间的关联,我请求您参考以下问题 - stackoverflow.com/q/59664765/4068817
    • 感谢我阅读了这篇文章并更新了我的问题,其中包含我的选择逻辑。
    【解决方案2】:

    好的,最后我发现在我要查询的类中,我需要在查询之前输入以下代码

    users.hasMany(chat_histories, {foreignKey: 'sender_id'});
    

    上面的代码是在我进行查询的类中定义的,而不是在 ORM 类中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-10
      • 2016-03-08
      • 2021-02-13
      • 1970-01-01
      • 2013-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多