【问题标题】:Sequlize how to use joined table's attribute name in a where clase续集如何在 where 类中使用连接表属性名称
【发布时间】:2018-08-24 10:11:16
【问题描述】:

我有三张桌子

  1. restaurant_items
  2. 餐厅
  3. 地区

我的协会是

餐厅有很多餐厅项目

restaurant_items 属于餐馆

餐厅属于地区

我想查询餐厅项目表以获取它们应该来自特定区域的项目。

这是我的代码。

const results = await db.restaurant_items.findAll({
            where: {
                region_id: 3
            },
            include: [{
                model: db.restaurants,
                attributes: ['region_id']
            }]
        })

Sequlize 给了我错误

未知列'restaurant_items.region_id

因为这与 restaurant_items 无关。它和餐厅的桌子在一起。但是我该如何实现呢?

【问题讨论】:

  • 请同时发布您的原始查询。
  • 当我将 where 子句放入包含数组时,代码有效。 =)

标签: mysql sequelize.js


【解决方案1】:

您可以在包含中添加 where 案例。试试

const results = await db.restaurant_items.findAll({
        include: [{
            model: db.restaurants,
            where: {
              region_id: 3
            }
        }]
    })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 2014-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多