如何将PostgreSQL转换为续集格式



我必须尝试使用sequelize npm软件包将我的帖子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;

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

错误:列位置departmapping.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
        });

续集具有关于如何使用ORM语法编写查询的非常好的文档。首先,以上查询看起来像

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

您应该考虑到的是
1.了解如何创建续集模型
2.学习协会
3.查询

那里有很多很棒的教程,可以帮助您入门,并且只是Google搜索!

最终帮助得到解决方案:

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在哪里条件包含表列

最新更新