Magic sql does not add table name in query?

Here's the sample:
const userSubQuery = sql<{ logo: string | null; email: string }>`
(
SELECT json_build_object(
'logo', ${users.logo},
'email', ${users.email}
)
FROM ${users}
WHERE ${users.id} = ${orders.userId}
)
`.as("user");
db.select(...getTableColumns(orders), user: userSubQuery).from(orders).where(eq(orders.id, 2))
const userSubQuery = sql<{ logo: string | null; email: string }>`
(
SELECT json_build_object(
'logo', ${users.logo},
'email', ${users.email}
)
FROM ${users}
WHERE ${users.id} = ${orders.userId}
)
`.as("user");
db.select(...getTableColumns(orders), user: userSubQuery).from(orders).where(eq(orders.id, 2))
The generated SQL is:
Query: select "id", ... , "updated_at", "expired_at",
(
SELECT json_build_object(
'logo', "logo",
'email', "email"
)
FROM "users"
WHERE "id" = "user_id"
)
as "user" from "orders" where "orders"."id" = $1 -- params: [2]
Query: select "id", ... , "updated_at", "expired_at",
(
SELECT json_build_object(
'logo', "logo",
'email', "email"
)
FROM "users"
WHERE "id" = "user_id"
)
as "user" from "orders" where "orders"."id" = $1 -- params: [2]
You can see the ${users.logo} not convert to "users"."logo", ${users.id} not convert to "users"."id", which will cause some queries not get the correct result. How could I solve it? I really don't like the pure SQL like SELECT json_build_object('logo', u.logo) FROM users u because this lack of IDE support (variables trace). Thanks.
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?