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