drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

[BUG]: unable to set timestamp field to be NOT NULL

Open janusqa opened this issue 2 years ago • 1 comments

What version of drizzle-orm are you using?

0.26.1

What version of drizzle-kit are you using?

0.18.0

Describe the Bug

import {
    mysqlTable,
    bigint,
    varchar,
    timestamp,
    primaryKey,
    uniqueIndex,
} from 'drizzle-orm/mysql-core';

export const usersTable = mysqlTable(
    'ts',
    {
        id: bigint('id', { mode: 'number' }).autoincrement().notNull(),
        email: varchar('email', { length: 125 }).notNull(),
        createdAt: timestamp('created_at').defaultNow().notNull(),
        updatedAt: timestamp('updated_at').defaultNow().onUpdateNow().notNull(),
    },
    (table) => ({
        emailUx: uniqueIndex('ux_ts_email').on(table.email),
        pk: primaryKey(table.id),
    })
);

Generated migrations

CREATE TABLE `ts` (
	`id` bigint AUTO_INCREMENT NOT NULL,
	`email` varchar(125) NOT NULL,
	`created_at` timestamp NOT NULL DEFAULT (now()),
	`updated_at` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	PRIMARY KEY(`id`)
);

CREATE UNIQUE INDEX `ux_ts_email` ON `ts` (`email`);

Error: Invalid default value for 'created_at'","timestamp"

Happens when running a migration. It seems to occur when specifying a uniqueIndex, but ok otherwise.

Expected behavior

expect not to get any error.

Environment & setup

No response

janusqa avatar May 29 '23 13:05 janusqa

`created_at` timestamp NOT NULL DEFAULT (now()),

If i remove the other parentheses that enclose now(), it seems to work. How can I do a generation without enclosing now() in parentheses.

I ended up switching from

createdAt: timestamp('created_at').defaultNow().notNull(),

to

createdAt: timestamp('created_at').default(sql`CURRENT_TIMESTAMP`).notNull(),

janusqa avatar Jun 02 '23 00:06 janusqa

We sill deprecate .defaultNow() and will introduce .defaultCurrentTimestamp() function which works in all cases.

Tracking issue #921

AndriiSherman avatar Jul 21 '23 17:07 AndriiSherman

What's the status of this? No sign of defaultCurrentTimestamp in current latest version (0.28.6)

dejoma avatar Sep 22 '23 13:09 dejoma

@dejoma looks like its still open https://github.com/drizzle-team/drizzle-orm/issues/921

janusqa avatar Sep 22 '23 16:09 janusqa