Error appending to array oOnConflictUpdaste using Drizzle ORM (Postgresql)

23 views Asked by At

My Typescript code using Drizzle ORM to insert into a 'users' table in a Postgres database:

const res = await db.insert(UsersTable)
    .values(newUser)
    .onConflictDoUpdate({ 
        target:UsersTable.email, 
        set: { 
            ipList:sql`array_append(${UsersTable.ipList}, ${newUser.ipList})`
        }
    })
    .returning({ insertedId: UsersTable.id });

This is what I'm trying to achieve (in SQL):

INSERT INTO users (email, ip_list, name, phone, is_admin, password)
VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT (email) DO UPDATE
SET ip_list = array_append(users.ip_list, $7::INET[])
RETURNING id;

IpList is an array (PgArray?) of the postgres datatype 'INET'.

The error is in the line:

set: { 
        ipList:sql`array_append(${UsersTable.ipList}, ${newUser.ipList})`
    }

If I remove it it works fine.

I also tried:

ipList:(sql`array_append(${UsersTable.ipList}, ${newUser.ipList})`)

It didn't work.

The issue seems to be with the internal raw SQL it's using.

I could use db.execute() with raw SQL but it would remove type safety.

Error message:

✓ Compiled /checkout in 682ms (1154 modules)
 ⨯ node_modules\@neondatabase\serverless\index.mjs (1339:61) @ eval
 ⨯ error: syntax error at or near ")"
    at async createUser (./app/lib/data/main.ts:63:17)
0

There are 0 answers