【问题标题】:how to avoid the error 42702 (AMBIGUOUS COLUMN ) with knex and postgres如何使用 knex 和 postgres 避免错误 42702 (AMBIGUOUS COLUMN)
【发布时间】:2020-10-26 16:02:12
【问题描述】:

我有两个表,类别和子类别

都有描述字段

我配置了以下查询

 const get = (req, res) => {
    app.db({ s: 'subCategory', c: 'category' })
        .select('s.id', 's.description', { category: 'c.description' })
        .whereRaw('?? = ??', ['c.id', 's.categoryId'])
        .where({ deletedAt: null })
        .then(subCategorys => res.json(subCategorys))
        .catch(err => res.status(500).send(err))
}

我重命名了类别列描述字段, 但它显示了重复列的错误,我有另一种方法,我以这种方式重命名并且它适用于那些具有相同名称的列,它显示错误 42702(这是列的模棱两可的错误)

我正在使用 postgresql 并将 knex 重命名为 db

这个方法有什么需要改变的,否则我真的需要用 knex.raw 创建整个查询?

错误: { "length": 111, "name": "error", "severity": "ERROR", "code": "42702", "position": "152", "file": "parse_relation.c", "line": "791", "routine": "colNameToVar" }

【问题讨论】:

  • 用表别名限定所有列名(如deletedat)。
  • 你是对的,我添加了.whereNull('s.deletedAt'),它成功了非常感谢你

标签: postgresql knex.js


【解决方案1】:

您需要用别名定义不明确的列。 如果deletedAt在表中不明确,则需要在查询中设置s.deletedAt

【讨论】:

    猜你喜欢
    • 2021-09-13
    • 2014-03-19
    • 2015-09-14
    • 2014-02-15
    • 1970-01-01
    • 2020-06-17
    • 1970-01-01
    • 2020-06-19
    • 2020-01-01
    相关资源
    最近更新 更多