drizzle-team / drizzle-team/drizzle-orm
[BUG]: Introspecting MySQL views generates invalid TypeScript for varchar columns (missing length)
- 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?
1.0.0-beta.20
### What version of `drizzle-kit` are you using?
1.0.0-beta.20
### Other packages
_No response_
### Describe the Bug
**What is the undesired behavior?**
When running `drizzle-kit pull` on a MySQL database that contains a View with a `VARCHAR` column, the generated schema file creates the view column as `varchar()` without the required `length` argument.
Since the MySQL `varchar` type in Drizzle requires a length configuration object, this generates invalid TypeScript code and breaks compilation with the error: `Expected 1-2 arguments, but got 0.`
**What are the steps to reproduce it?**
1. Create a MySQL table with a `VARCHAR` column and a view based on it:
```sql
CREATE TABLE base_table (
id INT,
name VARCHAR(50)
);
CREATE VIEW broken_view AS SELECT * FROM base_table;
```
2. Run drizzle-kit pull to introspect the database.
3. Check the generated schema.ts file.
The table is generated correctly (varchar({ length: 50 })), but the view is missing the length parameter:
```typescript
// Table is correct
export const baseTable = mysqlTable("base_table", {
id: int(),
name: varchar({ length: 50 }),
});
// View is broken
export const brokenView = mysqlView("broken_view", {
id: int(),
name: varchar(), // <--- TypeScript error here
}).as(sql`...`);
```
Also, you can use this [repo](https://github.com/danielcuque/mysql_drizzle) with `pnpm repro:crash`
**What is the desired result?**
The generated mysqlView should correctly infer the length of the string column and generate varchar({ length: 50 }), just like it does for base tables.
Root Cause
I was digging into the source code and found exactly why this happens. Inside the fromDatabase function for MySQL (where the // VIEWS block iterates over viewColumns), the mapping is using DATA_TYPE instead of COLUMN_TYPE.
In information_schema.columns, DATA_TYPE only returns "varchar", whereas COLUMN_TYPE returns "varchar(50)". Since the ddlToTypeScript generator only receives "varchar", it doesn't know the length.
Contributor guide
Assessment
This issue has not been assessed yet.