Skip to content
This repository was archived by the owner on Sep 3, 2025. It is now read-only.
This repository was archived by the owner on Sep 3, 2025. It is now read-only.

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

@nr1brolyfan

Description

@nr1brolyfan

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions