select array_to_json(coalesce(array_agg(row_to_json(t)), '{}')) from (SELECT books.id, books.name, books.user_id, (select array_to_json(coalesce(array_agg(row_to_json(t)), '{}')) from (SELECT favorites.id, favorites.user_id FROM "favorites" WHERE ("books"."id"="book_id")) t) "favorites", (select row_to_json(t) from (SELECT users.id, users.name FROM "users" WHERE ("id"="user_id")) t) "user" FROM "favorites" WHERE "favorites"."user_id" = 12345) t;
of which the important part is
(SELECT users.id, users.name FROM "users" WHERE ("id"="user_id"))
The user_id column is not scoped by table and it generates an ambiguous column error because there are two potentially matching columns (the user_id on Book and the user_id on Favorite).
This PR simply adds the table name where it is currently omitted to prevent any ambiguity.
Problem: surus-generated queries can contain ambiguous column errors if more than one table has the same column name.
For example, consider this schema:
And the query:
Surus will generate a query like:
of which the important part is
The
user_id
column is not scoped by table and it generates an ambiguous column error because there are two potentially matching columns (theuser_id
on Book and theuser_id
on Favorite).This PR simply adds the table name where it is currently omitted to prevent any ambiguity.