drizzle-team / drizzle-team/drizzle-orm
[BUG]: drizzle-kit pull crashes on MSSQL when tablesFilter excludes referenced FK table
- Dominant language
- TypeScript
- Stars
- 35.8k
- Forks
- 1.6k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 4
Description
### What version of `drizzle-orm` are you using?
`1.0.0-rc.4`
### What version of `drizzle-kit` are you using?
`1.0.0-rc.4`
### Other packages
- `mssql@12.6.0`
- Node.js `24.16.0`
### Describe the Bug
`drizzle-kit pull` with `dialect: "mssql"` crashes when `tablesFilter` includes a table that has a foreign key referencing another table outside the filter.
The connection and schema/table filtering work, but introspection fails while processing foreign keys:
```txt
TypeError: Cannot read properties of undefined (reading 'schema_id')
at drizzle-kit/bin.cjs:157687:75
```
In the TypeScript source for `drizzle-kit@1.0.0-rc.4`, this corresponds to `drizzle-kit/src/dialects/mssql/introspect.ts`:
```ts
for (const fk of groupedFkCostraints) {
const tableFrom = filteredTables.find((it) => it.object_id === fk.parent_table_id);
if (!tableFrom) continue;
const schemaFrom = filteredSchemas.find((it) => it.schema_id === fk.schema_id)!;
const tableTo = filteredTables.find((it) => it.object_id === fk.reference_table_id)!;
const schemaTo = filteredSchemas.find((it) => it.schema_id === tableTo.schema_id)!;
```
`fkCostraintQuery` fetches all foreign keys for the selected schemas:
```sql
FROM sys.foreign_keys fk
LEFT JOIN sys.foreign_key_columns fkc ON fkc.constraint_object_id = fk.object_id
WHERE fk.schema_id IN (...)
```
But `filteredTables` only contains tables that pass `tablesFilter`. If a filtered-in table has a FK to a filtered-out table, `tableFrom` exists and `tableTo` is `undefined`. The non-null assertion then causes the runtime error.
### Minimal reproduction
Run this on SQL Server in an empty database:
```sql
CREATE TABLE dbo.parent_table (
id int NOT NULL PRIMARY KEY,
child_id int NULL
);
CREATE TABLE dbo.referenced_table (
id int NOT NULL PRIMARY KEY
);
ALTER TABLE dbo.parent_table
ADD CONSTRAINT FK_parent_table_referenced_table
FOREIGN KEY (child_id) REFERENCES dbo.referenced_table(id);
```
Then configure pull with only the parent table:
```ts
export default {
dialect: 'mssql',
schemaFilter: ['dbo'],
tablesFilter: ['parent_table'],
dbCredentials: {
server: 'localhost',
port: 1433,
user: '...',
password: '...',
database: '...',
options: {
trustServerCertificate: true,
encrypt: false,
},
},
};
```
Run:
```bash
drizzle-kit pull --config drizzle.config.ts
```
Expected behavior:
- Introspect `parent_table` successfully.
- Either skip the FK whose referenced table is outside `tablesFilter`, or include enough metadata to handle it gracefully.
- Do not crash with an unhandled `TypeError`.
Actual behavior:
```txt
Cannot read properties of undefined (reading 'schema_id')
```
### Real-world context
This happens on an existing SQL Server ERP database where only a subset of tables is needed by the application. In that database:
- All selected tables exist in `dbo`.
- Many selected tables have foreign keys to other ERP tables that are outside the selected subset.
- Expanding `tablesFilter` to the outgoing FK closure avoids the crash, which confirms the problem is the filtered-out referenced table.
### Possible fix
The MSSQL introspector could apply the same table filter to FK processing, for example:
```ts
const tableTo = filteredTables.find((it) => it.object_id === fk.reference_table_id);
if (!tableTo) continue;
```
Or alternatively fetch/process only FKs where both parent and referenced tables are in the filtered result set.
Contributor guide
Assessment
This issue has not been assessed yet.