【问题标题】:How to convert postgreSQL to Sequelize format如何将 postgreSQL 转换为 Sequelize 格式
【发布时间】:2019-10-10 17:38:33
【问题描述】:

我必须尝试使用​​ 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;

按照下面的代码,我仍然得到了

错误:列 locationDepartmentMapping.department.departmentName 不存在

正如@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
        });

【问题讨论】:

  • 你已经定义了你的模型了吗?如果是这样,请将其包含在问题中。
  • @TheWildHealer,是的,已创建。 [link]docs.sequelizejs.com,你要查吗?
  • @TheWildHealer,感谢您指出连接错误。

标签: node.js postgresql npm sequelize.js


【解决方案1】:

最终在帮助下得到了解决方案:

let ldData = 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
    });

以下内容:

@shivam answer for joining tables

@ManjulSigdel answer for where condition include table column

【讨论】:

    【解决方案2】:

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

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

    您应该考虑进行上述查询的是
    1.学习如何创作sequelizemodels
    2.学习Associations
    3.Querying

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

    【讨论】:

    • 默认 where 组合运算符是OR?我认为您的查询可能会执行 b."departmentName" like '%Accounting%' and c."locationName" like '%Accounting%' 而不是 b."departmentName" like '%Accounting%' or c."locationName" like '%Accounting%'
    • @shivam,我们不需要内连接on a."departmentId" = b.id 查询?
    • Kitta:不,我们不必明确写出加入,这些都是关于你如何建立关联的注意事项,@TheWildHealer:是的,你是对的,这会做一个 AND。
    • 那么如何在两个关联模型之间有一个OR?我也很好奇。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 2022-11-21
    相关资源
    最近更新 更多