如何使用 knex 和 postgres 避免错误 42702 (AMBIGUOUS COLUMN)
how to avoid the error 42702 (AMBIGUOUS COLUMN ) with knex and postgres
我有两个表,类别和子类别
都有描述字段
我配置了以下查询
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
在表上不明确,您需要在查询中设置 s.deletedAt
我有两个表,类别和子类别
都有描述字段
我配置了以下查询
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
在表上不明确,您需要在查询中设置 s.deletedAt