簡體   English   中英

如何將postgreSQL轉換為Sequelize格式

[英]How to convert postgreSQL to Sequelize format

我必須嘗試使用​​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
        });

Sequelize有關於如何使用orm語法編寫查詢的相當好的文檔。 首先,上面的查詢看起來像

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

你應該考慮進入上述查詢是什么
1.學習如何創建續集模型
2.學習協會
3. 查詢

有很多很棒的教程,可以幫助你入門,只是一個谷歌搜索!

最終幫助得到了解決方案:

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連接表的答案

@ManjulSigdel回答條件包括表格列

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM