nextauthjs / next-auth

Authentication for the Web.
https://authjs.dev
ISC License
24.23k stars 3.37k forks source link

Drizzle Adapter Postgres Broken – The types of _.config.columns.id are incompatible between these types. #11490

Open FroeMic opened 1 month ago

FroeMic commented 1 month ago

Adapter type

@auth/drizzle-adapter

Environment

  System:
    OS: macOS 14.5
    CPU: (12) arm64 Apple M2 Max
    Memory: 3.54 GB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.17.1 - ~/.nvm/versions/node/v18.17.1/bin/node
    npm: 10.2.1 - ~/.nvm/versions/node/v18.17.1/bin/npm
    pnpm: 8.10.5 - ~/Library/pnpm/pnpm
  Browsers:
    Brave Browser: 126.1.67.119
    Chrome: 127.0.6533.73
    Safari: 17.5
  npmPackages:
    @auth/drizzle-adapter: ^1.1.0 => 1.4.2 
    next: ^14.2.4 => 14.2.5 
    next-auth: ^4.24.7 => 4.24.7 
    react: ^18.3.1 => 18.3.1 

Reproduction URL

https://github.com/FroeMic/t3-auth-test

Describe the issue

After running npm create t3-app@latest the NextAuth DrizzleAdapter throws a Typescript error in /src/server/auth.ts (see reproduction example.

It seems that the schemas generated by drizzle are not compatible with the ones the adapter expects. Some properties expected by the column type of the adapter are not provided by the schemas generated by drizzle.

Using the first example from the PostgreSQL schema.ts example from https://authjs.dev/getting-started/adapters/drizzle#schemas:

Schema

export const users = pgTable("user", {
  id: text("id")
    .primaryKey()
    .$defaultFn(() => crypto.randomUUID()),
  name: text("name"),
  email: text("email").unique(),
  emailVerified: timestamp("emailVerified", { mode: "date" }),
  image: text("image"),
})

Typescript Error (formatted)

Type 'PgTableWithColumns<{ 
  name: "user"; 
  schema: undefined; 
  columns: { 
    id: PgColumn<{ 
      name: "id"; 
      tableName: "user"; 
      dataType: "string"; 
      columnType: "PgText"; 
      data: string; 
      driverParam: string; 
      notNull: true; 
      hasDefault: true; 
      enumValues: [...]; 
      baseColumn: never; 
    }, {}, {}>; 
    name: PgColumn<...>; 
    email: PgColumn<...>; 
    ema... 
  } 
}>' is not assignable to type 'DefaultPostgresUsersTable'.

Type 'PgTableWithColumns<{ 
  name: "user"; 
  schema: undefined; 
  columns: { 
    id: PgColumn<{ 
      name: "id"; 
      tableName: "user"; 
      dataType: "string"; 
      columnType: "PgText"; 
      data: string; 
      driverParam: string; 
      notNull: true; 
      hasDefault: true; 
      enumValues: [...]; 
      baseColumn: never; 
    }, {}, {}>; 
    name: PgColumn<...>; 
    email: PgColumn<...>; 
    ema... 
  } 
}>' is not assignable to type 'PgTable<{ 
  name: string; 
  columns: { 
    id: DefaultPostgresColumn<{ 
      columnType: "PgVarchar" | "PgText" | "PgUUID"; 
      isPrimaryKey: true; 
      data: string; 
      notNull: true; 
      dataType: "string"; 
    }>; 
    name: DefaultPostgresColumn<...>; 
    email: DefaultPostgresColumn<...>; 
    emailVerified: DefaultPostgresColumn<...>; 
    image: DefaultPostgres... 
  }>'.

  The types of '_.config.columns.id' are incompatible between these types.

  Type 'PgColumn<{ 
    name: "id"; 
    tableName: "user"; 
    dataType: "string"; 
    columnType: "PgText"; 
    data: string; 
    driverParam: string; 
    notNull: true; 
    hasDefault: true; 
    enumValues: [string, ...string[]]; 
    baseColumn: never; 
  }, {}, {}>' is not assignable to type 'DefaultPostgresColumn<{ 
    columnType: "PgVarchar" | "PgText" | "PgUUID"; 
    isPrimaryKey: true; 
    data: string; 
    notNull: true; 
    dataType: "string"; 
  }>'.

  Type '{ 
    name: "id"; 
    tableName: "user"; 
    dataType: "string"; 
    columnType: "PgText"; 
    data: string; 
    driverParam: string; 
    notNull: true; 
    hasDefault: true; 
    enumValues: [string, ...string[]]; 
    baseColumn: never; 
  }' is missing the following properties from type '{ 
    name: string; 
    isAutoincrement: boolean; 
    isPrimaryKey: true; 
    hasRuntimeDefault: boolean; 
    generated: any; 
    columnType: "PgVarchar" | "PgText" | "PgUUID"; 
    data: string; 
    driverParam: string | number | boolean; 
    ... 4 more ...; 
    tableName: string; 
  }': 
    isAutoincrement, 
    isPrimaryKey, 
    hasRuntimeDefault, 
    generated

What does that mean? The DefaultPostgresColumn type definition in adapter-drizzle/src/lib/pg.ts expects for each column values (isAutoincrement, isPrimaryKey, hasRuntimeDefault, generated), that the schema generated by pgTable doesn't have.

How to reproduce

You should be able to reproduce the error by running npm create t3-app@latest with the above mentioned options and then opening/src/server/auth.ts and /src/server/db/schema.ts

Expected behavior

The Table / Column Definitions generated by Drizzle are compatible with the Adapter.

dca123 commented 1 month ago

I've temporarily resolved by using the previous patch "@auth/drizzle-adapter": "1.4.1"

nathanredblur commented 1 month ago

The hard part was to figure out how to ask about this issue in google. here a more detailed error description.

eaoliver commented 1 month ago

I had a similar issue, migrated back to 1.4.1. It went away when I upgraded drizzle-orm to version 0.33.0.

Q16solver commented 2 weeks ago

It also seems like the authenticators table doesn't include the PgUuid type for user id when it's a uuid, so an incompatibility there as well