drizzle-team / drizzle-team/drizzle-orm

[BUG]: Timestamp with null default, workaround quoted does not cater for generated SQL

Open
#2,492 3 comments 0 reactions 0 assignees View on GitHub
db/mysql drizzle/kit improvement
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

I just ran the following query in MySql 8:
```sql
CREATE TABLE `users` (
`id` serial AUTO_INCREMENT NOT NULL,
`verified_at` timestamp,
`deleted_at` timestamp,
CONSTRAINT `users_id` PRIMARY KEY(`id`)
);
```
And it works as expected. Drizzle supports the latest in MySql unfortunately. But you have a workaround:
```ts
export const users = mysqlTable('users', {
id: serial('id').default(sql`null`).primaryKey(),
verifiedAt: timestamp('verified_at').default(sql`null`).$type(),
deletedAt: timestamp('deleted_at').default(sql`null`).$type()
});
```

_Originally posted by @Angelelz in https://github.com/drizzle-team/drizzle-orm/issues/1136#issuecomment-1810232933_

workaround helps for typing in ide. although when i want to publish to my production database it errors at the timestamps, because the sql produces for timestamps with the default of null needs to have the "NULL" after the column datatype in some older mysql versions, any my hosting does not allow the option to change my mysql version.

Expected SQL statement to be generated:
...table declare start...
deleted_at timestamp NULL DEFAULT NULL,
...table declare end...

The provided solution only caters for code side and needs the fix as mentions in expected SQL above please

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.