Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: TypeError: Cannot read properties of undefined (reading 'checkConstraint') when trying to pull specifics tables from a schema #3884

Open
1 task done
guilhermepereira25 opened this issue Jan 2, 2025 · 4 comments · May be fixed by #4321
Assignees
Labels
bug Something isn't working drizzle/kit has-pr This issue has one or more PRs that that could close the issue when merged priority Will be worked on next

Comments

@guilhermepereira25
Copy link

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.38.3

What version of drizzle-kit are you using?

0.30.1

Other packages

No response

Describe the Bug

I am trying to pull a specific schema from my database and fetch only one table from that schema.

The process fails while fetching check constraints and returns the following error:

[✓] 1  tables fetched
[✓] 90 columns fetched
[✓] 35 indexes fetched
[✓] 0  foreign keys fetched
[⣟] 0  policies fetching
[⣟] 1  check constraints fetching
[✓] 0  views fetched
TypeError: Cannot read properties of undefined (reading 'checkConstraint') 
    at fromDatabase (node_modules/drizzle-kit/bin.cjs:36427:23)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

The error occurs when executing the query to fetch check constraints from the database:

  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';`
      );

The result of this query includes a table that does not exist in the result object, causing a TypeError.

The error occurs in the following code when attempting to assign the constraint name:

      for (const checkConstraintRow of checkConstraints) {
        const constraintName = checkConstraintRow['CONSTRAINT_NAME'];
        const constraintValue = checkConstraintRow['CHECK_CLAUSE'];
        const tableName = checkConstraintRow['TABLE_NAME'];
        const tableInResult = result[tableName];
        tableInResult.checkConstraint[constraintName] = {
          name: constraintName,
          value: constraintValue
        };
      }

I fixed this issue by checking if the tableName exists using a tablesFilter function before continuing the loop iteration:

      for (const checkConstraintRow of checkConstraints) {
        if (!tablesFilter(checkConstraintRow['TABLE_NAME']))
          continue;
        const constraintName = checkConstraintRow['CONSTRAINT_NAME'];
        const constraintValue = checkConstraintRow['CHECK_CLAUSE'];
        const tableName = checkConstraintRow['TABLE_NAME'];
        const tableInResult = result[tableName];
        tableInResult.checkConstraint[constraintName] = {
          name: constraintName,
          value: constraintValue
        };
      }

My suggestion is to only fetch constraints for tables that exist in the tablesFilter array by using an IN clause in the SQL query or by applying the solution above.

@guilhermepereira25 guilhermepereira25 added the bug Something isn't working label Jan 2, 2025
@L-Mario564 L-Mario564 added drizzle/kit priority Will be worked on next labels Jan 6, 2025
@L-Mario564 L-Mario564 self-assigned this Feb 4, 2025
@L-Mario564 L-Mario564 added the has-pr This issue has one or more PRs that that could close the issue when merged label Feb 4, 2025
@bfanger
Copy link

bfanger commented Feb 7, 2025

Logging the checkConstraintRow when I encountered this error the columns where there but in lowercase

Changing

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

into

const constraintName = checkConstraintRow["CONSTRAINT_NAME"] ?? checkConstraintRow["constraint_name"];
const constraintValue = checkConstraintRow["CHECK_CLAUSE"] ?? checkConstraintRow["check_clause"];
const tableName = checkConstraintRow["TABLE_NAME"] ?? checkConstraintRow["table_name"];

in bin.cjs solved the issue for me.

@PolarisPyra
Copy link

can confirm bfangers solution fixed it. Thank you. i hope this regression gets fixed soon as its a bit annoying to deal with in docker.

@yoanastamenova
Copy link

Boy this saved me from 2 hours ongoing nerves. Forever grateful <3

@laurilarjo
Copy link

laurilarjo commented Feb 26, 2025

I faced the same issue and bfanger's fix did it for me. Thank you!

UPDATE: And now when I tried using the tablesFilter, i encountered the OP's issue and his suggested fix worked for me.

mattico added a commit to mattico/drizzle-orm that referenced this issue Mar 26, 2025
…lumns

In certain circumstances the information_schema columns are lowercased from the db. This updates the mysqlSerializer to handle this case.

Fixes drizzle-team#3884
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working drizzle/kit has-pr This issue has one or more PRs that that could close the issue when merged priority Will be worked on next
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants