drizzle-team / drizzle-team/drizzle-orm

[BUG]: Drizzle Seed postgres one to one primary key references failing to generate entries

Open
#5,204 0 comments 1 reaction 0 assignees View on GitHub
bug drizzle/seed
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Report hasn't been filed before.

- [x] I have verified that the bug I'm about to report hasn't been filed before.

### What version of `drizzle-orm` are you using?

0.44.6

### What version of `drizzle-kit` are you using?

0.31.5

### Other packages

drizzle-seed@0.3.1

### Describe the Bug

We have tables where the primary id of one table is referencing another table. This relation is a one to one with it's primary key to another table. One more added element is that the one table is in another pg schema.

I am working in a `bun` *monorepo*

```
//auth-users.ts
export const auth = pgSchema("auth");

export const authUsers = auth.table("users", {
id: uuid("id").default(sql`pg_catalog.gen_random_uuid()`).primaryKey(),
});
```

```
// this is in the public schema

export const usersTable = pgTable(
"users",
{
id: uuid()
.primaryKey()
.references(() => authUsers.id, {
onDelete: "restrict",
onUpdate: "cascade",
}),
},
);

```

This causes an error generating test values with the following drizzle seed file

```
seed.ts

import { drizzle } from "drizzle-orm/node-postgres";
import { seed } from "drizzle-seed";

import { databaseEnv } from "./env";
import { schema } from "./schema";

async function main() {
const db = drizzle(databaseEnv.DATABASE_URL, { casing: "snake_case" });

const { aUsers, usersTable } = schema;

await seed(db, { authUsers, usersTable }, { count: 1 })
}

main();
```

```
➜ bun run src/seed.ts
36 | async queryWithCache(queryString, params, query) {
37 | if (this.cache === void 0 || is(this.cache, NoopCache) || this.queryMetadata === void 0) {
38 | try {
39 | return await query();
40 | } catch (e) {
41 | throw new DrizzleQueryError(queryString, params, e);
^
error: Failed query: insert into "users" ("id", "created_at", "updated_at", "email", "first_name", "surname", "mobile") values (default, $1, $2, $3, default, default, default)
params: 2025-10-05T02:58:01.393Z,2024-11-01T02:00:56.261Z,bored_geralynn@gmx.de
query: "insert into \"users\" (\"id\", \"created_at\", \"updated_at\", \"email\", \"first_name\", \"surname\", \"mobile\") values (default, $1, $2, $3, default, default, default)",
params: [
"2025-10-05T02:58:01.393Z", "2024-11-01T02:00:56.261Z", "bored_geralynn@gmx.de"
],

at queryWithCache (repo/node_modules/.bun/drizzle-orm@0.44.7+cc49a63d3679c3ad/node_modules/drizzle-orm/pg-core/session.js:41:15)

40 | res = resolve
41 | rej = reject
42 | }).catch((err) => {
43 | // replace the stack trace that leads to `TCP.onStreamRead` with one that leads back to the
44 | // application that created the query
45 | Error.captureStackTrace(err)
^
error: null value in column "id" of relation "users" violates not-null constraint
length: 277,
severity: "ERROR",
detail: "Failing row contains (null, 2025-10-05 02:58:01.393, 2024-11-01 02:00:56.261, bored_geralynn@gmx.de, null, null, null).",
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: "public",
table: "users",
dataType: undefined,
constraint: undefined,
file: "execMain.c",
routine: "ExecConstraints",
code: "23502"

at (repo/node_modules/.bun/pg-pool@3.10.1+635858982ab829dd/node_modules/pg-pool/index.js:45:11)

Bun v1.3.1 (macOS arm64)
```

This works in generating a test `auth.users` but doesn't generate the corresponding `public.users` entry. I know it wouldn't but this is just to show case that the generation of the `auth.users` schema table works.

```
seed.ts

async function main() {
const db = drizzle(databaseEnv.DATABASE_URL, { casing: "snake_case" });

const { authUsers } = schema;

await seed(db, { authUsers }, { count: 1 })
}

main();
```

My expectation here would be that the `auth.users` entry is generated first then the `public.users` entry.

To me the problem seems to be that the `public.users` id value is not being carried over from the `auth.users`. Perhaps this is because the `auth.users` entry is not being generated first?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.