© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•13mo ago•
3 replies
JuaniS

COALESCE to json array not working in sql function

I am using the sql operator in a postgres db and when I try to coalesece to an empty array it doesn't work as expected. It returns null.
This is how I am doing it. Is it wrong?
 const productItemsImagesSQLAgg = sql<ProductItemImages[]>`
    COALESCE(
      JSON_AGG(
        JSON_BUILD_OBJECT(
          'url', ${images.url}, 
          'key', ${images.key}
        )
      ) FILTER (WHERE ${images.id} IS NOT NULL), 
    '[]'::JSON
    )
    `.as('images');
 const productItemsImagesSQLAgg = sql<ProductItemImages[]>`
    COALESCE(
      JSON_AGG(
        JSON_BUILD_OBJECT(
          'url', ${images.url}, 
          'key', ${images.key}
        )
      ) FILTER (WHERE ${images.id} IS NOT NULL), 
    '[]'::JSON
    )
    `.as('images');
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

utility function for COALESCE
Drizzle TeamDTDrizzle Team / help
14mo ago
Custom SQL function (json_agg & json_build_object)
Drizzle TeamDTDrizzle Team / help
3y ago
Unable to pass JSON array to PostgreSQL function jsonb[] parameter using execute raw SQL
Drizzle TeamDTDrizzle Team / help
15mo ago
coalesce
Drizzle TeamDTDrizzle Team / help
2y ago