[BUG]: unable to set timestamp field to be NOT NULL
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
`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(),
We sill deprecate .defaultNow() and will introduce .defaultCurrentTimestamp() function which works in all cases.
Tracking issue #921
What's the status of this? No sign of defaultCurrentTimestamp in current latest version (0.28.6)
@dejoma looks like its still open https://github.com/drizzle-team/drizzle-orm/issues/921