Custom column type with default not working?

I tried to add a new column with a custom type and a default value. but for some reason when I tried to db push, it warned me that it didn't have a default value and that it would truncate the tables. Am I doing something wrong?
const unsignedBigInt = customType<{
data: number;
// I also tried this ↓ https://orm.drizzle.team/docs/custom-types#examples
// notNull: true;
// default: true;
}>({
dataType() {
return "bigint UNSIGNED";
},
});

export const myTable = mysqlTable("MyTable", {
id: varchar("id", { length: 191 }).primaryKey().notNull(),
//...
// ↓ this was added ↓
usage: unsignedBigInt("usage").default(0).notNull(),
});
const unsignedBigInt = customType<{
data: number;
// I also tried this ↓ https://orm.drizzle.team/docs/custom-types#examples
// notNull: true;
// default: true;
}>({
dataType() {
return "bigint UNSIGNED";
},
});

export const myTable = mysqlTable("MyTable", {
id: varchar("id", { length: 191 }).primaryKey().notNull(),
//...
// ↓ this was added ↓
usage: unsignedBigInt("usage").default(0).notNull(),
});
· You're about to add not-null usage column without default value, which contains 3 items
· You're about to add not-null usage column without default value, which contains 3 items
3 Replies
Ravi
RaviOP2y ago
I decided to say Yes to the warning to see what happens. The drizzle-kit truncated the table, but the table seems to have the correct default value.
mysql> describe MyTable;
+-----------+-----------------+------+-----+----------------------+-------------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-----------------+------+-----+----------------------+-------------------+
| id | varchar(191) | NO | PRI | NULL | |
-- ...
| usage | bigint unsigned | NO | | 0 | |
+-----------+-----------------+------+-----+----------------------+-------------------+
mysql> describe MyTable;
+-----------+-----------------+------+-----+----------------------+-------------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-----------------+------+-----+----------------------+-------------------+
| id | varchar(191) | NO | PRI | NULL | |
-- ...
| usage | bigint unsigned | NO | | 0 | |
+-----------+-----------------+------+-----+----------------------+-------------------+
Now if I add some data and run db push again (without doing any changes), it thinks the data is serial and it wants to truncate it again.
Warning Found data-loss statements:
· You're about to change usage column type from serial to bigint UNSIGNED with 3 items

THIS ACTION WILL CAUSE DATA LOSS AND CANNOT BE REVERTED
Warning Found data-loss statements:
· You're about to change usage column type from serial to bigint UNSIGNED with 3 items

THIS ACTION WILL CAUSE DATA LOSS AND CANNOT BE REVERTED
Ravi
RaviOP2y ago
Found the GitHub issue on this. https://github.com/drizzle-team/drizzle-orm/issues/818 Sorry that I didn't search there first..
GitHub
[BUG]: Column with custom type not working with default() · Issue...
What version of drizzle-orm are you using? v0.27.0 What version of drizzle-kit are you using? v0.19.2 Describe the Bug With the following schema: import { customType, mysqlTable, serial, varchar } ...
kelbs
kelbs2y ago
I think the root issue is that drizzle doesn't support unsigned ints. I'm guessing you wouldn't be creating custom types if unsigned ints were supported

Did you find this page helpful?