FG
๐Ÿ’ป Software๐Ÿ—„๏ธ Databases

[BUG]: TypeError: Cannot read properties of undefined (reading 'compositePrimaryKeys')

Fresh3 days ago
Mar 14, 20260 views
Confidence Score77%
77%

Problem

What version of `drizzle-orm` are you using? 0.30.10 What version of `drizzle-kit` are you using? 0.21.2 Describe the Bug if I have tables with no composite primary keys(table => pk: primaryKey(....) , then i can change my column names and do whatever i want with no issue in push. issues arises when: changing column names, adding new columns, tables to schema that any table has composite primary key. if schema already exists in database and i want to add composite primary key. everytime before pushing new version of the schema, i have to drop all my tables in db. otherwise `drizzle-kit push` command keeps failing with this error. Expected behavior _No response_ Environment & setup next.js 14 app router application

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix โ€“ Awaiting Verification

Fix TypeError by Ensuring Composite Primary Key Handling in Drizzle ORM

Medium Risk

The error occurs because the `drizzle-orm` library does not handle schema changes involving composite primary keys correctly when the existing schema in the database conflicts with the new schema. Specifically, when a table with a composite primary key is altered (e.g., changing column names or adding new columns), the ORM fails to read the properties of the schema correctly, leading to the TypeError.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Backup Database

    Before making any changes, ensure that you have a complete backup of your database to prevent data loss.

    bash
    pg_dump -U username -h localhost dbname > db_backup.sql
  2. 2

    Drop Existing Tables with Composite Keys

    If you need to change the schema that involves composite primary keys, drop the existing tables in the database that have composite primary keys before pushing the new schema.

    sql
    DROP TABLE IF EXISTS your_table_name CASCADE;
  3. 3

    Update Schema Definition

    Modify your schema definition in your Drizzle ORM setup to include the new composite primary keys and any other changes you need.

    typescript
    export const yourTable = pgTable('your_table_name', { pk: compositePrimaryKey('column1', 'column2'), ... });
  4. 4

    Push New Schema

    Run the `drizzle-kit push` command to apply the new schema to the database after ensuring that the old tables have been dropped.

    bash
    npx drizzle-kit push
  5. 5

    Verify Schema Changes

    After pushing the new schema, verify that the changes have been applied correctly by checking the database schema and ensuring that the composite primary keys are set as expected.

    sql
    SELECT * FROM information_schema.table_constraints WHERE table_name = 'your_table_name';

Validation

Confirm that the `drizzle-kit push` command completes without errors and that the database schema reflects the new structure with the composite primary keys correctly defined.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

drizzleormtypescriptbugdrizzle/kitpriority