Repository navigation
Why does the same query work using raw sql but not the select method?
#4666
Replies: 1 comment
|
The error comes from Drizzle’s selected-column check. Keep import { getTableColumns, sql } from "drizzle-orm";
const name = "hello";
const query = db
.select(getTableColumns(items))
.from(items)
.innerJoin(
sql`json_each(${items.tags}) AS "jsonTags"`,
sql`true`,
)
.where(sql`"jsonTags"."value" ->> '$.name' = ${name}`);
const rows = await query;The join condition If your intent is instead “return each item once if any tag matches,” use a correlated const rows = await db
.select()
.from(items)
.where(sql`exists (
select 1
from json_each(${items.tags}) AS "jsonTags"
where "jsonTags"."value" ->> '$.name' = ${name}
)`);That deliberately avoids duplicate item rows when several tags match; the join version preserves those duplicates. The SQLite dialect’s check explains the error. I also checked that the 0.44.2 join implementation accepts SQL expressions, alongside SQLite’s |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
When I perform the query using the following syntax it works:
However the same query using
selectthrows an error:The error:
Is this a bug or am I just not using
selectcorrectly?PS: This is using SQLite with a json field.
All reactions