考虑如下查询:
select
row_to_json(t)
from (
select
tablex.id,
valuetable.id as "valuetable.id",
valuetable.val_string as "valuetable.val_string"
FROM TableA AS tablex
INNER JOIN TableB AS valuetable ON tablex.id = valuetable.id
WHERE tablex.code= '123')t;
当从postgres命中时,返回的响应为
{id:111,valuetable.id:222,valuetable.val_string:"xx"}
当我运行相同的查询时:
select
tablex.id,
valuetable.id as "valuetable.id",
valuetable.val_string as "valuetable.val_string"
FROM TableA AS tablex
INNER JOIN TableB AS valuetable ON tablex.id = valuetable.id
WHERE tablex.code= '123'
带有sequelize,它的响应为
当我运行相同的查询时:
{id:111,valuetable:{id:222,val_string:"xx"}}
这是预期的。
请问您也喜欢如何从postgres生成这样的响应,还是需要从Java端进行任何操作?
答案 0 :(得分:0)
尝试使用 json_build_object 代替row_to_json,如下所示:
select
json_build_object(
'id', tablex.id,
'valuetable', json_build_object(
'id', valuetable.id,
'val_string', valuetable.val_string)
)
FROM TableA AS tablex
INNER JOIN TableB AS valuetable ON tablex.id = valuetable.id
WHERE tablex.code= '123'