next-auth icon indicating copy to clipboard operation
next-auth copied to clipboard

Wrong type for primary key with @auth/drizzle-adapter and Postgres

Open benjibenoit opened this issue 2 years ago • 5 comments

Adapter type

@auth/drizzle-adapter

Environment

System:
    OS: Linux 5.15 Debian GNU/Linux 12 (bookworm) 12 (bookworm)
    CPU: (24) x64 AMD Ryzen 9 5900X 12-Core Processor
    Memory: 10.40 GB / 15.58 GB
    Container: Yes
    Shell: 5.2.15 - /bin/bash
Binaries:
    Node: 20.9.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.1.0 - /usr/local/bin/npm
npmPackages:
    @auth/drizzle-adapter: ^0.3.7 => 0.3.7 
    next: ^14.0.1 => 14.0.2 
    next-auth: 5.0.0-beta.3 => 5.0.0-beta.3 
    react: ^18 => 18.2.0

Describe the issue

In the doc : https://authjs.dev/reference/adapter/drizzle

We can see id: text("id").notNull().primaryKey(),. The problem is that for postgres, we have "serial" like : id: serial("id").primaryKey(),

If a c/p the code from the doc I got this error : PostgresError: operator does not exist: text = integer I tried to set every xxxId to integer and I got this error PostgresError: invalid input syntax for type integer: "b3c867ae-a7c4-46d4-b6e6-256a6cc90b4b"

How to reproduce

Install package and c/p the schema from the doc : https://authjs.dev/reference/adapter/drizzle

Expected behavior

The schema should maybe use serial("id").primaryKey() and integer for xxxId ?

benjibenoit avatar Nov 21 '23 11:11 benjibenoit

hey do u fix this problem?, should i just change id from text to serial?

gonsura avatar Feb 06 '24 09:02 gonsura

There needs to be some mechanism by which you can use serial integer instead of the uuid string. Please add the functionality out-of-the-box

RutuparnDalvi95 avatar Mar 22 '24 12:03 RutuparnDalvi95

Same issue if the schema is defined with "uuid" instead of serial/text: id: uuid("id").primaryKey().notNull().defaultRandom(),

Ideally the type check on the fields would be more flexible and allow serial/uuid too.

elbajo avatar Apr 21 '24 21:04 elbajo

Could anyone resolve this? I'm doing something like:

id: varchar('id', { length: 255 })
      .notNull()
      .primaryKey()
      .$defaultFn(() => crypto.randomUUID())

But would rather use serial...

flaviogragnolati avatar Jun 29 '24 14:06 flaviogragnolati