drizzle-team / drizzle-team/drizzle-orm
[BUG]: MySQL introspection crashes on functional/expression indexes (COLUMN_NAME is null)
- Dominant language
- TypeScript
- Stars
- 35.8k
- Forks
- 1.6k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 4
Description
### Report hasn't been filed before.
- [x] 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.45.2
### What version of `drizzle-kit` are you using?
0.31.10
### Other packages
_No response_
### Describe the Bug
**What is the undesired behavior?**
`drizzle-kit pull` crashes with a `TypeError: Cannot read properties of null (reading 'camelCase')` when introspecting a MySQL 8+ database that contains a unique functional/expression index.
In MySQL 8, when an index is created using an expression (e.g., `CREATE UNIQUE INDEX idx ON table ((CASE WHEN col = 1 THEN 1 ELSE NULL END))`), the `COLUMN_NAME` field in `INFORMATION_SCHEMA.STATISTICS` returns `NULL`. Instead, the actual expression is stored in the `EXPRESSION` column.
Currently, `mysqlSerializer.ts` unconditionally pushes `COLUMN_NAME` into the index constraints. Later, during TS generation in `introspect-mysql.ts`, `casing(null)` is called, crashing the CLI.
**What are the steps to reproduce?**
I have created a minimal reproduction repository for this issue:
[Replicated error]
(https://github.com/danielcuque/mysql_drizzle)
1. Clone the repo and run `pnpm install`
2. Run `pnpm run db:up` to start the MySQL container.
3. Run `pnpm run repro:index` (which targets a table with a functional index).
4. (Assuming the silent crash bug is patched locally) You will see the `TypeError` originating from `createTableUniques` in `introspect-mysql.ts`.
**What is the desired result?**
Drizzle should extract the `EXPRESSION` column from `INFORMATION_SCHEMA.STATISTICS` when `COLUMN_NAME` is null. It should flag these columns in the `internals` object, and the TS generators should output them wrapped in `sql\`...\`` instead of crashing.
Contributor guide
Assessment
This issue has not been assessed yet.