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 · 0 comments
Labels
bug Something isn't working drizzle/kit 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
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 priority Will be worked on next
Projects
None yet
Development

No branches or pull requests

2 participants