better-auth-kit icon indicating copy to clipboard operation
better-auth-kit copied to clipboard

Seeding error: insert or update on table violates foreign key constraint

Open nr1brolyfan opened this issue 1 year ago • 2 comments

Running the seeding script throws a Postgres error.

// seed.ts
import { Seed, users, organizations, apiKeys } from "@better-auth-kit/seed";

export const seed = Seed({
  ...users(),
  ...organizations(),
  ...apiKeys(),
});

Error:

bun x @better-auth/cli@latest generate --config ./src/server/fake-auth.ts --output ./src/server/default-schema.ts
...
✓ Finished seeding user with 100 rows.                                                                      
Seeding session with 100 rows...                                                                            

2025-06-07T15:47:21.836Z WARN [Better Auth]: [Drizzle Adapter] - You are trying to create a record with an id. This is not allowed as we handle id generation for you, unless you pass in the `forceAllowId` parameter. The id will be ignored.
Error
    at Object.execute (file:///.../repo/node_modules/@better-auth-kit/seed/dist/index.js:1:1406103)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async P (file:///.../AppData/Local/Temp/bunx-2971375518-@better-auth-kit/cli@latest/node_modules/@better-auth-kit/cli/dist/index.js:9:6457)
    at async Command.kv (file:///.../AppData/Local/Temp/bunx-2971375518-@better-auth-kit/cli@latest/node_modules/@better-auth-kit/cli/dist/index.js:9:5916)

Error seeding "session" in model session:

DrizzleQueryError: Failed query: insert into "session" ("id", "expires_at", "token", "created_at", "updated_at", "ip_address", "user_agent", "user_id", "impersonated_by", "active_organization_id") values ($1, $2, $3, $4, $5, $6, $7, $8, default, default) returning "id", "expires_at", "token", "created_at", "updated_at", "ip_address", "user_agent", "user_id", "impersonated_by", "active_organization_id"
params: Kv0yYf4aZgrgIpFQvqNxOz4iE2S2rAZc,2020-12-07T00:43:02.821Z,4f807da7-0172-458f-a044-11d51b824e61,2015-07-12T21:49:20.243Z,2029-12-17T18:04:22.285Z,80.2.2.89,Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36,2c715aa7-8586-4400-b5f6-d4c3ebbf5181
    at PostgresJsPreparedQuery.queryWithCache (file:///.../repo/node_modules/drizzle-orm/pg-core/session.js:42:15)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async file:///.../repo/node_modules/drizzle-orm/postgres-js/session.js:37:20
    at async withReturning (file:///.../repo/node_modules/better-auth/dist/adapters/drizzle-adapter/index.mjs:49:19)
    at async Object.create (file:///.../repo/node_modules/better-auth/dist/adapters/drizzle-adapter/index.mjs:176:26)
    at async Object.create (file:///.../repo/node_modules/better-auth/dist/shared/better-auth.Dpv9J4ny.mjs:393:19)
    at async Object.execute (file:///.../repo/node_modules/@better-auth-kit/seed/dist/index.js:1:1406095)
    at async P (file:///.../AppData/Local/Temp/bunx-2971375518-@better-auth-kit/cli@latest/node_modules/@better-auth-kit/cli/dist/index.js:9:6457)
    at async Command.kv (file:///.../AppData/Local/Temp/bunx-2971375518-@better-auth-kit/cli@latest/node_modules/@better-auth-kit/cli/dist/index.js:9:5916) {
  query: 'insert into "session" ("id", "expires_at", "token", "created_at", "updated_at", "ip_address", "user_agent", "user_id", "impersonated_by", "active_organization_id") values ($1, $2, $3, $4, $5, $6, $7, $8, default, default) returning "id", "expires_at", "token", "created_at", "updated_at", "ip_address", "user_agent", "user_id", "impersonated_by", "active_organization_id"',
  params: [
    'Kv0yYf4aZgrgIpFQvqNxOz4iE2S2rAZc',
    '2020-12-07T00:43:02.821Z',
    '4f807da7-0172-458f-a044-11d51b824e61',
    '2015-07-12T21:49:20.243Z',
    '2029-12-17T18:04:22.285Z',
    '80.2.2.89',
    'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
    '2c715aa7-8586-4400-b5f6-d4c3ebbf5181'
  ],
  cause: PostgresError: insert or update on table "session" violates foreign key constraint "session_user_id_user_id_fk"
      at ErrorResponse (file:///.../repo/node_modules/postgres/src/connection.js:794:26)
      at handle (file:///.../repo/node_modules/postgres/src/connection.js:480:6)
      at Socket.data (file:///.../repo/node_modules/postgres/src/connection.js:315:9)
      at Socket.emit (node:events:524:28)
      at addChunk (node:internal/streams/readable:561:12)
      at readableAddChunkPushByteMode (node:internal/streams/readable:512:3)
      at Readable.push (node:internal/streams/readable:392:5)
      at TCP.onStreamRead (node:internal/stream_base_commons:189:23) {
    severity_local: 'ERROR',
    severity: 'ERROR',
    code: '23503',
    detail: 'Key (user_id)=(2c715aa7-8586-4400-b5f6-d4c3ebbf5181) is not present in table "user".',
    schema_name: 'public',
    table_name: 'session',
    constraint_name: 'session_user_id_user_id_fk',
    file: 'ri_triggers.c',
    line: '2599',
    routine: 'ri_ReportViolation'
  }
}

error: script "auth:seed" exited with code 1

I'm testing on a locally set up postgres docker container.

nr1brolyfan avatar Jun 07 '25 18:06 nr1brolyfan

Just confirming this error using Drizzle and sqlite. I am getting both of the above errors on created a record with an id and to use forceAllowId as well as the foreign key constrant error. I tried adding forceAllowId to databaseHooks>user>create but it did not work. I am not sure where to add forceAllowId or if that is just a bug. Users are actually being added to the database.

delebash avatar Aug 03 '25 10:08 delebash

Workaround (ish)

Same issue for me. I just need some users so i did this. This will not create sessions and accounts. Database: sqlite using drizzle libsql driver

import { Seed, users } from '@better-auth-kit/seed'

export const seed = Seed({
  ...users(
    {
      user: {
        id: undefined,
      },
    },
    { count: 100, createAccounts: false, createSessions: false },
  ),
})

chanithecat avatar Aug 22 '25 07:08 chanithecat