Mysql 删除联接查询结果中的前缀tablename
使用nodejs(10.15.0)、sequelize(4.42.0)和MySQL,我在join查询中尝试从结果中删除表路径Mysql 删除联接查询结果中的前缀tablename,mysql,node.js,sequelize.js,sequelize-typescript,Mysql,Node.js,Sequelize.js,Sequelize Typescript,使用nodejs(10.15.0)、sequelize(4.42.0)和MySQL,我在join查询中尝试从结果中删除表路径 Table1 .findAll({ attributes: ['id', 'name', 'other'], include: [{ attributes: ['code_id'], model: Table2, nested: false, required: true,
Table1
.findAll({
attributes: ['id', 'name', 'other'],
include: [{
attributes: ['code_id'],
model: Table2,
nested: false,
required: true,
}],
raw: true,
})
查询结果
[
{
"id": 1,
"name": "stuff",
"other": true,
"table2.code_id": 1,
}
]
期待发生
[
{
"id": 1,
"name": "stuff",
"other": true,
"code_id": 1,
}
]
Remove
raw:true,
-它阻止Sequelize将结果解析为对象。如果不想使用模型实例,则需要为结果编写自己的解析器
请注意,它将解析为以下结构(“table2”将是一个属性):
或者,您可以为子行设置别名,但请注意,除非创建映射到该行的虚拟字段,否则它将进入dataValues
Table1
.findAll({
attributes: [
'id', 'name', 'other',
[sequelize.col('table2.code_id'), 'code_id'], // aliased here
],
include: [{
attributes: [],
model: Table2,
required: true,
}],
raw: true,
})
Remove
raw:true,
-它阻止Sequelize将结果解析为对象。如果不想使用模型实例,则需要为结果编写自己的解析器
请注意,它将解析为以下结构(“table2”将是一个属性):
或者,您可以为子行设置别名,但请注意,除非创建映射到该行的虚拟字段,否则它将进入dataValues
Table1
.findAll({
attributes: [
'id', 'name', 'other',
[sequelize.col('table2.code_id'), 'code_id'], // aliased here
],
include: [{
attributes: [],
model: Table2,
required: true,
}],
raw: true,
})
尝试将
添加为:“code\u id”
作为属性添加到包含
-array中的对象。尝试将添加为:“code\u id”
作为属性添加到包含
-array中的对象。这不会作为错误“未知列”表2。字段列表中的“代码”因为table2不是db table name,它是一个模型名。@NoorFathima我的示例中没有“代码”,所以我认为您有输入错误。也许你有“code”而不是“code_id”?我完全按照你的要求做了,我得到了SequelizeDatabaseError:multi-part identifier“…”无法绑定
。这不是错误“未知列”table2。“字段列表”中的“code”,因为table2不是db表名,而是一个模型名。@Noorbathima我没有“code”在我的例子中的任何地方,所以我认为你有一个打字错误。也许你有“代码”而不是“代码id”?我完全按照你的要求做了,我得到了SequelizeDatabaseError:多部分标识符“…”无法绑定
。