KyselyK
Kysely2y ago
tzezar

concat in postgres

Hey! I want to concat few columns to get address, however query is built in wrong way cuz postgres uses
||
to concat. What am I doing wrong?

let warehouses = await db
      .selectFrom('warehouse as w')
      .leftJoin('address as a', 'w.addressId', 'a.id')
      .select((eb) => [
          'w.id',
          'w.name',
          'w.description',
          eb.fn<string>('concat', [
              'a.city',
              eb.val(' '),
              'a.district',
              eb.val(' '),
              'a.street',
          ]).as('address')
      ])
      .execute()


select
    "w"."id",
    "w"."name",
    "w"."description",
    concat("a"."city",
    $1,
    "a"."district",
    $2,
    "a"."street") as "address"
from
    "warehouse" as "w"
left join "address" as "a" on
    "w"."address_id" = "a"."id"
',
  parameters: [ ' ', ' ' ]
I made it with raw sql for now
sql<string>`concat(a.city || ' ' || a.street)`.as('address')
but I wonder what is proper way
Was this page helpful?