How do I create a fk to a uuid column?

My schema:
export const overlaysTable = pgTable("overlays", {
id: uuid("id").notNull().defaultRandom().primaryKey(),
ownerId: varchar("owner_id")
.notNull()
.references(() => usersTable.id, { onDelete: "cascade" }),
name: varchar("name").notNull(),
status: varchar("status").$type<StatusOptions>().notNull(),
type: varchar("type").$type<OverlayType>().notNull(),
rewardId: varchar("reward_id"),
});

export const queTable = pgTable("clipQue", {
id: uuid("id").notNull().defaultRandom().primaryKey(),
overlayId: uuid("overlay_id")
.notNull()
.references(() => overlaysTable.id, { onDelete: "cascade" }),
clipId: varchar("clip_id").notNull(),
});
export const overlaysTable = pgTable("overlays", {
id: uuid("id").notNull().defaultRandom().primaryKey(),
ownerId: varchar("owner_id")
.notNull()
.references(() => usersTable.id, { onDelete: "cascade" }),
name: varchar("name").notNull(),
status: varchar("status").$type<StatusOptions>().notNull(),
type: varchar("type").$type<OverlayType>().notNull(),
rewardId: varchar("reward_id"),
});

export const queTable = pgTable("clipQue", {
id: uuid("id").notNull().defaultRandom().primaryKey(),
overlayId: uuid("overlay_id")
.notNull()
.references(() => overlaysTable.id, { onDelete: "cascade" }),
clipId: varchar("clip_id").notNull(),
});
When I try to push this I get error: column "id" cannot be cast automatically to type uuid
2 Replies
TheDanniCraft
TheDanniCraftOP3mo ago
any help?
JustWayne
JustWayne3mo ago
The error is only happening during push which is a migration right? So perhaps you can inspect the SQL that gets generated for your migrations and see if you can find the issue there, perhaps by running the changes manually It sounds to me like you've changed the type of data type for id or overlay_id. So you might have to manually alter your migration sql files to do an extra step. Searching for pg error cannot be cast automatically to type uuid shows a lot of results for how to deal with it in general. My bad, it says here - https://orm.drizzle.team/docs/drizzle-kit-push
drizzle-kit push lets you literally push your schema and subsequent schema changes directly to the database while omitting SQL files generation
So it doesn't generate SQL files for push. So I would recommend using drizzle-kit generate instead so you can see what's going on. I don't know how it will work for a project that previously used push.

Did you find this page helpful?