Skip to content

[BUG]: drizzle-kit pull fails because of incorrect check constraint query #3627

Description

@gainggs

Report hasn't been filed before.

  • 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.36.4

What version of drizzle-kit are you using?

0.28.1

Other packages

No response

Describe the Bug

When I perform this command:

drizzle-kit pull

I receive the following output:

Pulling from ['public'] list of schemas

[✓] 73  tables fetched
[✓] 607 columns fetched
[✓] 52  indexes fetched
[✓] 8   foreign keys fetched
[⣷] 0   policies fetching
[⣷] 8   check constraints fetching
[✓] 0   views fetched
TypeError: Cannot read properties of undefined (reading 'checkConstraint')
    at fromDatabase (C:\Users\User\Desktop\my-project\node_modules\drizzle-kit\bin.cjs:19001:23)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)

I've investigated and am able to fix this issue by making 2 changes to drizzle-kit/src/serializer/mysqlSerializer.ts:

1: Update the query which is retrieving the check constraints

From:

const checkConstraints = await db.query(
  `SELECT
    tc.table_name,
    tc.constraint_name,
    cc.check_clause
  FROM
    information_schema.table_constraints tc
    JOIN information_schema.check_constraints cc
      ON tc.constraint_name = cc.constraint_name
  WHERE tc.constraint_schema = '${inputSchema}'
    AND tc.constraint_type = 'CHECK';`
);

To:

const checkConstraints = await db.query(
  `SELECT
    tc.table_name,
    tc.constraint_name,
    cc.check_clause
  FROM
    information_schema.table_constraints tc
    JOIN information_schema.check_constraints cc
      ON tc.constraint_name = cc.constraint_name
      AND tc.constraint_schema = cc.constraint_schema
  WHERE tc.constraint_schema = '${inputSchema}'
    AND tc.constraint_type = 'CHECK';`
);

This helps because I have duplicate backup databases on the same server which means the original query was doing JOINs across several constraint schemas instead of limiting the JOIN to just the required constraint schema.

2: Since my server was returning lowercase column names in the results I also had to change:

const constraintName = checkConstraintRow['CONSTRAINT_NAME'];
const constraintValue = checkConstraintRow['CHECK_CLAUSE'];
const tableName = checkConstraintRow['TABLE_NAME'];

To:

const constraintName = checkConstraintRow['CONSTRAINT_NAME'] || checkConstraintRow['constraint_name'];
const constraintValue = checkConstraintRow['CHECK_CLAUSE'] || checkConstraintRow['check_clause'];
const tableName = checkConstraintRow['TABLE_NAME'] || checkConstraintRow['table_name'];

After these changes, the drizzle-kit pull command worked correctly.

For context, I'm running:

  • Windows 11
  • Node v22.11.0
  • MariaDB 11.5

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingbug/fixed-in-betaThis bug has been fixed in beta (or will be soon).db/mysqldrizzle/kitpriorityWill be worked on next

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions