drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

[BUG]: json_array_length not working with multiple subqueries

Open johanneskares opened this issue 2 years ago • 2 comments

What version of drizzle-orm are you using?

0.26.3

What version of drizzle-kit are you using?

No response

Describe the Bug

It's no longer possible to return multiple subqueries when using json_array_length.

In a previous bug report, there was another issue with multiple subqueries: https://github.com/drizzle-team/drizzle-orm/issues/599

After the fix, now I'm getting an error when using

where: (table) => sql`json_array_length(${table.projects}) > 0`

To replicate using the example from the original bug report and change it to:

db.query.tenant.findFirst({
  where: eq(tenant.id, "..."),
  with: {
    users: true,
    projects: true,
  },
  where: (table) => sql`json_array_length(${table.projects}) > 0`,
});

I'm getting: function json_array_length(text) does not exist. It works using 0.26.0.

Expected behavior

No error

Environment & setup

No response

johanneskares avatar Jun 01 '23 11:06 johanneskares

True, the query was changed a bit. You can use cast(<column> as json) to work around that.

dankochetov avatar Jun 01 '23 12:06 dankochetov

fantastic, good to know

sql`json_array_length(cast (${table.projects} as json)) > 0`,

did the trick

johanneskares avatar Jun 01 '23 14:06 johanneskares