【问题标题】:Sequelize makes wrong query when I make the fieldName to be changed and include it当我更改 fieldName 并包含它时,Sequelize 会进行错误查询
【发布时间】:2019-11-11 22:41:58
【问题描述】:

我的一个 Sequelize 模型看起来像这样,

Sequelize.define('unit', {
  ...
  complex_id: { // 'complex_id' is a key to use in my script
    field: 'building_id', // There's a field named 'building_id' in my 'complex' table
    reference: {
      model: 'complex',
      key: 'id'
    },
  },
  building_id: { // 'building_id' is a key to use in my script
    field: 'immeuble_id', // There's a field named 'immeuble_id' in my 'immeuble' table
    reference: {
      model: 'building',
      key: 'id',
    },
  },
})

因为 building_id 会让人困惑,但它背后有一个很长的故事。

我试图找到字段“complex_id”具有特定值的单元,这意味着单元表中名为“building_id”的字段。

直接选择单位就好了

unit.findAndCount({
  where: { complex_id: 123 }
})
// => SELECT * FROM unit WHERE building_id = 123

但是,当我尝试选择另一个实体并包含该单元时,它以奇怪的方式工作

another_entity.findAndCount({
  where: {  },
  include: [{
    model:unit,
    where: { complex_id: 123 },
  }],
})
// => SELECT * FROM onother_entity INNER JOIN unit ON .... AND immeble_id = 123

Sequelize 将“complex_id”转换为“building_id”。

但它不会停止转换,直到将 'complex_id' 更改为 'immeuble_id'

我不知道为什么 Sequelize 会这样。我应该怎么做才能使其按预期工作?

【问题讨论】:

    标签: node.js sequelize.js


    【解决方案1】:

    试试这个

    another_entity.findAndCount({
      where: {"$unit.complex_id$": 123},
      include: [unit]
    })
    

    【讨论】:

      猜你喜欢
      • 2018-03-09
      • 1970-01-01
      • 1970-01-01
      • 2020-06-17
      • 2019-04-26
      • 2018-05-13
      • 1970-01-01
      • 2018-07-23
      • 2019-11-18
      相关资源
      最近更新 更多