drizzle-team / drizzle-team/drizzle-orm

[BUG]: Introspection fails if SQLite database has a `references` without column name

Open
#3,231 1 comment 2 reactions 0 assignees View on GitHub
bug db/sqlite drizzle/kit
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?

?

### What version of `drizzle-kit` are you using?

?

### Describe the Bug

From drizzle-kit-mirror repo: https://github.com/drizzle-team/drizzle-kit-mirror/issues/100.

> I ran introspection on an existing large SQLite database I had and ran into an error. I finally managed to narrow it down to the fact that the error happens when there is a `references` column constraint with only a table name specified, no column name.
>
> Here are two databases:
>
> ```
> $ sqlite3 a.db .schema
> CREATE TABLE users(id integer primary key);
> CREATE TABLE posts(user_id integer references users);
> $ sqlite3 b.db .schema
> CREATE TABLE users(id integer primary key);
> CREATE TABLE posts(user_id integer references users(id));
> ```
>
> Introspecting `a.db` fails:
>
> ```
> $ pnpm drizzle-kit introspect:sqlite --url a.db --driver better-sqlite
> drizzle-kit: v0.19.2
> drizzle-orm: v0.27.0
>
> [✓] 2 tables fetched
> [✓] 2 columns fetched
> [✓] 0 enums fetched
> [✓] 0 indexes fetched
> [✓] 1 foreign keys fetched
> /private/tmp/foo/node_modules/.pnpm/drizzle-kit@0.19.2/node_modules/drizzle-kit/index.cjs:36446
> return value.camelCase();
> ^
>
> TypeError: Cannot read properties of null (reading 'camelCase')
> at withCasing2 (/private/tmp/foo/node_modules/.pnpm/drizzle-kit@0.19.2/node_modules/drizzle-kit/index.cjs:36446:22)
> at /private/tmp/foo/node_modules/.pnpm/drizzle-kit@0.19.2/node_modules/drizzle-kit/index.cjs:36613:18
> at Array.map ()
> at /private/tmp/foo/node_modules/.pnpm/drizzle-kit@0.19.2/node_modules/drizzle-kit/index.cjs:36598:37
> at Array.forEach ()
> at createTableColumns2 (/private/tmp/foo/node_modules/.pnpm/drizzle-kit@0.19.2/node_modules/drizzle-kit/index.cjs:36591:15)
> at /private/tmp/foo/node_modules/.pnpm/drizzle-kit@0.19.2/node_modules/drizzle-kit/index.cjs:36492:22
> at Array.map ()
> at schemaToTypeScript2 (/private/tmp/foo/node_modules/.pnpm/drizzle-kit@0.19.2/node_modules/drizzle-kit/index.cjs:36479:61)
> at sqliteIntrospect (/private/tmp/foo/node_modules/.pnpm/drizzle-kit@0.19.2/node_modules/drizzle-kit/index.cjs:45305:18)
>
> Node.js v18.4.0
> ```
>
> while doing the same on `b.db` succeeds:
>
> ```
> $ pnpm drizzle-kit introspect:sqlite --url b.db --driver better-sqlite
> drizzle-kit: v0.19.2
> drizzle-orm: v0.27.0
>
> [✓] 2 tables fetched
> [✓] 2 columns fetched
> [✓] 0 enums fetched
> [✓] 0 indexes fetched
> [✓] 1 foreign keys fetched
>
> [✓] Your SQL migration file ➜ drizzle/0000_motionless_leech.sql 🚀
> [✓] You schema file is ready ➜ drizzle/schema.ts 🚀
> ```
>
> I think the expected behavior is that if there is no column name, then the referenced column is the primary key column of that table.

### Expected behavior

_No response_

### Environment & setup

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.