drizzle-team / drizzle-team/drizzle-orm

[BUG]: Drizzle kit cuts off some fields of postgres dbCredentials

Open Beginner friendly
#6,229 0 comments 0 reactions 0 assignees View on GitHub
bug
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?

1.0.0-rc.4

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

1.0.0-rc.4

### Other packages

_No response_

### Describe the Bug

I have a multi-host postgres setup, where one prelica is a primary and the others are read-only. Postgres.js supports this and has a `target_session_attrs` field allowing to specify that we want to connect to the primary replica

See: https://github.com/porsager/postgres#multi-host-connections---high-availability-ha

This works for the actual app (because we can configure the client ourselves), but fails for the drizzle-kit migrations

So when I try to run migrations, I sometimes get this error, when the first host in the multihost string is not a primary replica
```
PostgresError: cannot execute CREATE SCHEMA in a read-only transaction
```

This happens because when drizzle-kit validates the credentials using zod, it silently deletes fields that are not described by the schema

https://github.com/drizzle-team/drizzle-orm/blob/main/drizzle-kit/src/cli/validations/postgres.ts

In my case the `target_session_attrs` field gets erased, but there are many more fields that could be useful for someone that also get erased

Here we can find definition for the type passed as options to the postgres function:
https://github.com/porsager/postgres/blob/411429e7bd7a3d61155ca9a70a97c111823702ea/types/index.d.ts#L346
which extends
https://github.com/porsager/postgres/blob/411429e7bd7a3d61155ca9a70a97c111823702ea/types/index.d.ts#L26

---

Questions

Q: What is the undesired behavior?
A: The `target_session_attrs` gets deletes in drizzle-kit, so the postgres client is initialized wrong and migrations can fail

Q: What are the steps to reproduce it?
A: Create a multi-host postgres setup, where the first host is a read-only replica. Try to apply migrations

Q: What is the desired result?
A: Either let me pass all the parameters I need for the client initialization or let me pass an initialized client in the `drizzle.config.ts`

Q: What database engine are you using? Are you using a specific cloud provider? Which one?
A: I'm using the `postgres` (postgres.js) engine. I'm not using a specific cloud provider

Q: Do you think this bug pertains to a specific database driver? Which one?
A: The bug is with the `postgres` (postgres.js) driver, but maybe there a similar issues with other ones

Q: Are you working in a monorepo?
A: No

Q: If this is a bug related to types: What Typescript version are you using? What's the content of your tsconfig.json file?
A: No. TS v6.0.3
```json
{
"compilerOptions": {
"esModuleInterop": true,
"skipLibCheck": true,
"target": "es2024",
"allowJs": true,
"resolveJsonModule": true,
"moduleDetection": "force",
"isolatedModules": true,
"verbatimModuleSyntax": true,

"strict": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,

"module": "preserve",
"noEmit": true,
"allowImportingTsExtensions": true,

"lib": ["es2024"],
"types": ["node"]
}
}

```

Q: If you're using a runtime that isn't Node.js: Which one? What version? Have you verified that this isn't an issue with the runtime itself?
A: I'm using Node.js v24.19. The issue is not with the runtime

---

I can open a PR adding the missing fields to
https://github.com/drizzle-team/drizzle-orm/blob/main/drizzle-kit/src/cli/validations/postgres.ts

Will happily do it if this makes this issue resolve faster. Currently have to patch drizzle-kit to work around the issue in my service

Contributor guide

Open the contributing guide

Research direction

Start in drizzle-kit/src/cli/validations/postgres.ts and compare its credential schema with the postgres.js options definitions linked in the issue. Reproduce the multi-host setup with a read-only first host, then verify that target_session_attrs and the needed credential fields survive validation and migrations can connect to the primary.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgres, postgresql, typescript
Domain
databases
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.