gpt4 book ai didi

node.js - 同时使用 'Where' 和 'Include' 语句拉取数据

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

我已经设法让我的包含语句与我的外键一起工作,但是当我尝试向 findAll 语句添加“where”语句时,我收到以下错误。我已经多次检查我的外键和模型,我看不出它们有任何问题。
下面的 API 中的语法是否正确?键中的数据很重要,所以我不想花更多时间在解决方法上。我已经看到其他示例在其中一个包含键中包含 where 语句,但我想要提取的字段位于主表上,所以我认为这不是相同的情况。

Unhandled rejection Error: Include unexpected. Element has to be either a Model, an Association or an object.


API
const sequelize = require("pg");
const { Sequelize, Op, Model, DataTypes } = require("sequelize");

exports.schoolDiveLogApproval = (req, res) => {

try {
const schoolID = req.params.schoolID;

diveLog.findAll({
include: [{
model: diveType, as: 'diveTypeID_FK2',
}, {
model: diveSchool, as: 'diveSchoolID_FK',
}, {
model: current, as: 'currentID_FK',
}, {
model: visibility, as: 'visibilityID_FK',
}, {
model: user, as: 'userID_FK1',
}, {
model: diveSpot, as: 'diveSpotID_FK2'
}],
where: {
[Op.and]: [
{diveSchoolID: schoolID},
{diveVerifiedBySchool: false}
]
},
})

...........

更新
错误信息
{
"message": "missing FROM-clause entry for table \"diveLog\""
}
带有原始 SQL 的 Controller API
exports.schoolDiveLogApproval = async (req, res) => {

try {
const schoolID = req.params.schoolID;
const { QueryTypes } = require('sequelize');
await diveLog.sequelize.query(
'SELECT * '+
'FROM "diveLogs" '+
'LEFT OUTER JOIN "diveTypes" AS "diveTypeID_FK2" ON "diveLogs"."diveTypeID" = "diveTypeID_FK2"."diveTypeID" ' +
'LEFT OUTER JOIN "diveSchools" AS "diveSchoolID_FK" ON "diveLog"."diveSchoolID" = "diveSchoolID_FK"."diveSchoolID" ' +
'LEFT OUTER JOIN "currents" AS "currentID_FK" ON "diveLog"."currentID" = "currentID_FK"."currentID" ' +
'LEFT OUTER JOIN "visibilities" AS "visibilityID_FK" ON "diveLog"."visibilityID" = "visibilityID_FK"."visibilityID" ' +
'LEFT OUTER JOIN "userLogins" AS "userID_FK" ON "diveLog"."userID" = "userID_FK1"."userID" ' +
'LEFT OUTER JOIN "diveSpots" AS "diveSpotID_FK2" ON "diveLog"."diveSpotID" = "diveSpotFK2"."diveSpotID" ' +
'WHERE "diveLogs"."diveSchoolID" = ? ' +
'AND "diveLogs"."diveVerifiedBySchool" = ?',
{
replacements: [schoolID, false],
type: QueryTypes.SELECT
}
)
根据 pgAdmin 数据库,所有表名和列名看起来都是正确的,所以除了语法之外,我看不出问题出在哪里。
我已经偏离了在 IDE 终端中执行 SQL 的方式,因此它应该在理论上执行。这更有可能与我传递 id 的方式有关吗?

最佳答案

好吧,我真的在这个 Sequelize 上尝试了几分钟,但我真的建议你学习和使用原始 SQL,它比这更容易和通用。
在尝试设置和理解这个 sqlize 半小时后,看到了很多像这样的奇怪问题:
enter image description here
选择不存在的列,如id,在表“user_infoS(?)”的名称中添加一个“s”,如果我理解你的问题,我放弃并想出了一个可能对你有帮助的查询正确。
如果您发送正确的架构,我可以进一步帮助您,但是,对不起,我不能花更多时间在这个 Sequelize 中,这件事太糟糕了。

const { QueryTypes } = require('sequelize');
await sequelize.query(
'SELECT *'+
'FROM diveLog l'+
'JOIN diveType t ON l.diveTypeId = t.id'+
'JOIN diveSchool s ON l.diveSchoolId = s.id'+
'JOIN current c ON l.currentId = c.id'+
'JOIN visibility v ON l.visibilityId = v.id'+
'JOIN user u ON l.userId = u.id'+
'JOIN diveSpot sp ON l.diveSpotId = sp.id'+
'WHERE diveSchoolID = ?'+
'AND diveVerifiedBySchool = ?',
{
replacements: [schoolID, false],
type: QueryTypes.SELECT
}
);

关于node.js - 同时使用 'Where' 和 'Include' 语句拉取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67419862/

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