Issue: Error during `db:push` - TypeError in Drizzle Kit with PostgreSQL

Hi everyone, I'm encountering a persistent issue when running the pnpm db:push command to push my PostgreSQL schema using drizzle-kit. The process fails with a TypeError related to replace() in [email protected]. Below are the details: #### Error Output:
$ pnpm db:push

> dotenv drizzle-kit push

No config path provided, using default 'drizzle.config.ts'
Reading config file 'D:\WORK\PEACOCK\A2Z-CARS\a2z-cars-cms\drizzle.config.ts'
Using 'postgres' driver for database querying
[⢿] Pulling schema from database...
D:\WORK\PEACOCK\A2Z-CARS\a2z-cars-cms\node_modules\.pnpm\[email protected]\node_modules\drizzle-kit\bin.cjs:19716
checkValue = checkValue.replace(/^CHECK\s*\(\(/, "").replace(/\)\)\s*$/, "");
^

TypeError: Cannot read properties of undefined (reading 'replace')
at D:\WORK\PEACOCK\A2Z-CARS\a2z-cars-cms\node_modules\.pnpm\d[email protected]\node_modules\drizzle-kit\bin.cjs:19716:39
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Node.js v18.20.4
 ELIFECYCLE  Command failed with exit code 1.
$ pnpm db:push

> dotenv drizzle-kit push

No config path provided, using default 'drizzle.config.ts'
Reading config file 'D:\WORK\PEACOCK\A2Z-CARS\a2z-cars-cms\drizzle.config.ts'
Using 'postgres' driver for database querying
[⢿] Pulling schema from database...
D:\WORK\PEACOCK\A2Z-CARS\a2z-cars-cms\node_modules\.pnpm\[email protected]\node_modules\drizzle-kit\bin.cjs:19716
checkValue = checkValue.replace(/^CHECK\s*\(\(/, "").replace(/\)\)\s*$/, "");
^

TypeError: Cannot read properties of undefined (reading 'replace')
at D:\WORK\PEACOCK\A2Z-CARS\a2z-cars-cms\node_modules\.pnpm\d[email protected]\node_modules\drizzle-kit\bin.cjs:19716:39
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Node.js v18.20.4
 ELIFECYCLE  Command failed with exit code 1.
#### My Setup: - Drizzle ORM: v0.35.3 - Drizzle Kit: v0.26.2 - Postgres: v3.4.4 - Node.js: v18.20.4 #### Drizzle Config (drizzle.config.ts):
import { env } from "@/env.js";
import { type Config } from "drizzle-kit";

import { database_prefix } from "@/lib/constants";

export default {
schema: "./src/db/schema/index.ts",
dialect: "postgresql",
out: "./drizzle",
dbCredentials: {
url: env.DATABASE_URL,
},
tablesFilter: [`${database_prefix}_*`],
verbose: true,
strict: true,
} satisfies Config;
import { env } from "@/env.js";
import { type Config } from "drizzle-kit";

import { database_prefix } from "@/lib/constants";

export default {
schema: "./src/db/schema/index.ts",
dialect: "postgresql",
out: "./drizzle",
dbCredentials: {
url: env.DATABASE_URL,
},
tablesFilter: [`${database_prefix}_*`],
verbose: true,
strict: true,
} satisfies Config;
No description
2 Replies
Jackson Kasi
Jackson KasiOP5w ago
#### What I've Tried: 1. Schema Inspection for CHECK Constraints: - I ran a query to check for any CHECK constraints in my PostgreSQL schema, as I suspected the error might be related to them. Here's what I found:
SELECT conname, pg_get_constraintdef(oid) as constraint_definition
FROM pg_constraint
WHERE contype = 'c';

SELECT conname, pg_get_constraintdef(oid) as constraint_definition
FROM pg_constraint
WHERE contype = 'c';

Sample results:
{
"constraint_name": "pgsodium_raw",
"schema_name": "pgsodium",
"table_name": "key",
"constraint_definition": "CHECK (CASE WHEN (raw_key IS NOT NULL) THEN ((key_id IS NULL) AND (key_context IS NULL) AND (parent_key IS NOT NULL)) ELSE ((key_id IS NOT NULL) AND (key_context IS NOT NULL) AND (parent_key IS NULL)) END)"
}

{
"constraint_name": "pgsodium_raw",
"schema_name": "pgsodium",
"table_name": "key",
"constraint_definition": "CHECK (CASE WHEN (raw_key IS NOT NULL) THEN ((key_id IS NULL) AND (key_context IS NULL) AND (parent_key IS NOT NULL)) ELSE ((key_id IS NOT NULL) AND (key_context IS NOT NULL) AND (parent_key IS NULL)) END)"
}

I suspect this (or a similar CHECK) constraint might be causing the issue. 2. Tried Downgrading/Upgrading drizzle-kit: I tried downgrading and upgrading drizzle-kit but the issue persists with the same error. 3. Checked Permissions: I also ensured that my PostgreSQL user has sufficient permissions to modify the schema and drop constraints. #### Next Steps: - Is there a known issue with drizzle-kit handling complex CHECK constraints like the one in pgsodium.key? - Should I manually drop these constraints before running db:push, or is there a workaround to handle this within Drizzle ORM? - Any insights or potential fixes from the community would be greatly appreciated! Thanks in advance for your help!
4nZ
4nZ5w ago
Hey @Jackson Kasi , I have faced the same error, when updating my schema, (pnpm drizzle-kit pull). I think instead of using the 'postgres' driver to query the DB, try using 'pg' driver to query. Doing so solved my issue. https://www.npmjs.com/package/pg
npm
pg
PostgreSQL client - pure javascript & libpq with the same API. Latest version: 8.13.0, last published: a month ago. Start using pg in your project by running npm i pg. There are 10200 other projects in the npm registry using pg.
Want results from more Discord servers?
Add your server