0

I have the following 2 tables in node.js:

users

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

However, every time when I do query in chat_history table I have the following error message:

SequelizeEagerLoadingError: chat_histories is not associated to users!

Is there anything wrong?

I can see myself have already associated chat_historeis with users table via adding the following in chat_histories.js

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

And added the following in 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'});
}}

At the following is how I do search for SQL statement:

select distinct users from users, chat_histories where (users.id = chat_histories.sender_id or users.id = chat_histories.receiver_id) and 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;
});
Zhang Zhan
  • 815
  • 8
  • 27

2 Answers2

0

You have not defined associations properly. Associations need to be created for both users and chat_histories table.

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

I have never used keywords like references and referencesKey in Sequelize models to define associations. I neither find any reference for them in the documentation.

Hope it helps!

Soham Lawar
  • 1,165
  • 1
  • 12
  • 20
  • Thank you for the reply! I have tried with adding: 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' }); to both users.js and chat_histiroes.js but still get the same error – Zhang Zhan Jan 14 '20 at 09:48
  • I request you to check what query sequelize generates after specifying associations as above. To get query you can use sequelize sync. Please refer the following question for more details https://stackoverflow.com/questions/21066755/how-does-sequelize-sync-work-specifically-the-force-option – Soham Lawar Jan 14 '20 at 09:56
  • I updated my original post a bit with more details about the class, also I included the association on both classes. Please have a look. Thank you. – Zhang Zhan Jan 14 '20 at 10:02
  • To understand how to define associations among sequelize models I request you to take reference from the following question - https://stackoverflow.com/q/59664765/4068817 – Soham Lawar Jan 14 '20 at 10:17
  • Thanks I have read the post and updated my question with my select logic included. – Zhang Zhan Jan 14 '20 at 10:45
0

ok at the end I figured out that in the class where I want to do the query, I need to put the following code before doing the query

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

The above code is defined in the class where I did the query instead of in ORM class

Zhang Zhan
  • 815
  • 8
  • 27