drizzle-team / drizzle-team/drizzle-orm
[BUG]: Incorrect inference for PostgreSQL array columns (arrays may contain null items)
- 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?
0.31.4
### What version of `drizzle-kit` are you using?
0.22.8
### Describe the Bug
In PostgreSQL, it's possible to include NULL values in array columns. See the example below:
```sql
create table example (
id serial primary key,
numbers integer[] not null
);
insert into example (numbers) values ('{1, 2, null, 4}');
select unnest(numbers) from example;
```
```
1
2
4
```
However, the inferred types from the ORM do not account for this.
```ts
const example = pgTable('example', {
id: serial('id').primaryKey(), // number
numbers: integer('numbers').array(), // number[] <-- incorrect, should be (number | null)[]
})
```
- This appears to be true for all columns that allow `.array()` usage.
- Apparently it's impossible to create non-nullable array columns in Postgres, so `.array()` should probably union `null` no matter what.
---
As a workaround, you can use `.$type<...>()`, for example:
```ts
const example = pgTable('example', {
id: serial('id').primaryKey(), // number
numbers: integer('numbers').array().$type<(number | null)[]>(), // (number | null)[] <-- correct!
})
```
Contributor guide
Assessment
This issue has not been assessed yet.