lucia-auth / lucia

Authentication, simple and clean
https://lucia-auth.com
MIT License
8.96k stars 460 forks source link

Types of property 'hasDefault' are incompatible. Type 'true' is not assignable to type 'false'. #1388

Closed deadcoder0904 closed 7 months ago

deadcoder0904 commented 7 months ago

I learned about ulidx today & tried using it in all id's by default like this:

import {
  sqliteTable,
  text,
  integer,
  uniqueIndex,
} from 'drizzle-orm/sqlite-core'
import { relations } from 'drizzle-orm'
import { ulid } from 'ulidx'

export const userTable = sqliteTable('user', {
  id: text('id')
    .primaryKey()
    .$defaultFn(() => ulid()),
  email: text('email').unique().notNull(),
  emailVerified: integer('email_verified').notNull(),
})

export const sessionTable = sqliteTable('session', {
  id: text('id')
    .primaryKey()
    .$defaultFn(() => ulid()),
  userId: text('user_id')
    .notNull()
    .references(() => userTable.id, {
      onUpdate: 'cascade',
      onDelete: 'cascade',
    }),
  expiresAt: integer('expires_at').notNull(),
})

The first one in userTable works fine but the second one in sessionTable gives me this error:

Argument of type 'SQLiteTableWithColumns<{ name: "session"; schema: undefined; columns: { id: SQLiteColumn<{ name: "id"; tableName: "session"; dataType: "string"; columnType: "SQLiteText"; data: string; driverParam: string; notNull: true; hasDefault: true; enumValues: [...]; baseColumn: never; }, object>; userId: SQLiteColumn<...>; e...' is not assignable to parameter of type 'SQLiteSessionTable'. Type 'SQLiteTableWithColumns<{ name: "session"; schema: undefined; columns: { id: SQLiteColumn<{ name: "id"; tableName: "session"; dataType: "string"; columnType: "SQLiteText"; data: string; driverParam: string; notNull: true; hasDefault: true; enumValues: [...]; baseColumn: never; }, object>; userId: SQLiteColumn<...>; e...' is not assignable to type 'SQLiteTable<{ dialect: any; columns: { id: SQLiteColumn<{ dataType: any; notNull: true; enumValues: any; tableName: any; columnType: any; data: string; driverParam: any; hasDefault: false; name: any; }, object>; expiresAt: SQLiteColumn<...>; userId: SQLiteColumn<...>; }; schema: any; name: any; }>'. The types of '_.config.columns.id' are incompatible between these types. Type 'SQLiteColumn<{ name: "id"; tableName: "session"; dataType: "string"; columnType: "SQLiteText"; data: string; driverParam: string; notNull: true; hasDefault: true; enumValues: [string, ...string[]]; baseColumn: never; }, object>' is not assignable to type 'SQLiteColumn<{ dataType: any; notNull: true; enumValues: any; tableName: any; columnType: any; data: string; driverParam: any; hasDefault: false; name: any; }, object>'. Type '{ name: "id"; tableName: "session"; dataType: "string"; columnType: "SQLiteText"; data: string; driverParam: string; notNull: true; hasDefault: true; enumValues: [string, ...string[]]; baseColumn: never; }' is not assignable to type '{ dataType: any; notNull: true; enumValues: any; tableName: any; columnType: any; data: string; driverParam: any; hasDefault: false; name: any; }'. Types of property 'hasDefault' are incompatible. Type 'true' is not assignable to type 'false'.ts(2345)

I get this error on sessionTable here:

const adapter = new DrizzleSQLiteAdapter(db, sessionTable, userTable)

I found the source to be:

https://github.com/lucia-auth/lucia/blob/ac8b7750dcc2beb039c72993720ea54f59007bf1/packages/adapter-drizzle/src/drivers/sqlite.ts#L142

Curious, why is it set to false? It means we can't pass a default id to session. What if we want to pass ulid like I did above? I think setting it to boolean might get the same result, no? Unless its used in someplaces Idk.

Just a minor gripe because all my ids used ulid so I thought this one should too for consistencies sake.

pilcrowOnPaper commented 7 months ago

Default database values are not supported in Lucia

id: text('id')
    .primaryKey()
    .$defaultFn(() => ulid()) // here

Instead, manually generate and pass the ID when creating the user

db.insertInto(userTable).values({
  id: ulid()
})
deadcoder0904 commented 7 months ago

@pilcrowOnPaper it works for userTable by default. it doesn't work for sessionTable.

even works for emailVerificationTokenTable. probably bcz lucia only requires 2 tables: userTable & sessionTable.

i don't think i can pass ulid() to sessionTable in any way as it gets set via lucia.createSession() i think.

pilcrowOnPaper commented 7 months ago

Yeah session ID can't be altered