gpt4 book ai didi

node.js - 如何将 postgreSQL 转换为 Sequelize 格式

转载 作者:行者123 更新时间:2023-12-03 22:37:46 35 4
gpt4 key购买 nike

我必须尝试使用​​ sequelize npm 包将我的 postgre sql 查询转换为 orm 概念,请指导我。

select * from "locationDepartmentMappings" as a
inner join "departments" as b on a."departmentId" = b.id
inner join "locations" as c on a."locationId" = c.id
where (
b."departmentName" like '%Accounting%' or c."locationName" like '%Accounting%'
)
limit 1;

根据下面的代码我仍然得到

error: column locationDepartmentMapping.department.departmentName does not exist



正如@shivam 提到的,我在下面尝试过取决于我的,你能不能我需要改变什么,
        let ldv = await LocationDepartmentModel.findAll({
include: [
{
model: LocationModel,
as: "location",
required: true,
},
{
model: DepartmentModel,
as: "department",
required: true,
}
],
where: {
$or: [
{
"department.departmentName": { like: `%${reqQueryName}%` }
},
{ "location.locationName": { like: `%${reqQueryName}%` } }
]
},
limit:1
});

最佳答案

Sequelize 有关于如何使用 orm 语法编写查询的很好的文档。
首先,上面的查询看起来像

Model.locationDepartmentMappings.findAll({
include: [
{
model: Model.departments
where: {departmentName: {$like: '% Accounting%'}}
},
{
model: Model.locations
where: {locationName: {$like: '% Accounting%'}}
}],
limit: 1
})

您应该考虑进入上述查询的是
1. 学习如何创建 sequelize models
2. 学习 Associations
3. Querying

那里有很多很棒的教程,可以帮助您入门,而且只需在 google 上搜索即可!

关于node.js - 如何将 postgreSQL 转换为 Sequelize 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56287197/

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