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