我在节点.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我都会收到以下错误消息:
续集急切加载错误:chat_histories与用户无关!
有什么问题吗?
我可以看到自己已经通过在chat_histories.js中添加以下内容chat_historeis与用户表相关联
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' });
}}
并在用户中添加了以下内容.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其中 (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;
});
您没有正确定义关联。需要为表users
和chat_histories
创建关联。
users.hasMany(models.chat_histories, { foreignKey: 'sender_id', as: 'chat_histories' });
chat_histories.belongsTo(models.users, { foreignKey: 'sender_id', as: 'user' });
我从未在 Sequelize 模型中使用过references
和referencesKey
等关键字来定义关联。我在文档中都没有找到它们的任何参考。
希望对您有所帮助!
最后我发现在我想进行查询的类中,我需要在执行查询之前输入以下代码
users.hasMany(chat_histories, {foreignKey: 'sender_id'});
上面的代码是在我执行查询的类中定义的,而不是在ORM类中定义的