Cast columns in relations

A great feature of drizzle's relational queries is that they don't actually require a foreign key, which provides a great amount of flexibility in querying your data. However, it doesn't seem like there is currently a way to cast the fields so that their types match. Something along the lines of
export const actionRelations = relations(actions, ({ many, one }) => ({
user: one(users, {
fields: [sql<string>`${actions.ownerId}::uuid`.as("ownerId")],
references: [users.id],
})
service: one(services, {
fields: [actions.ownerId],
references: [sql<string>`${services.id}::text`.as("id")]
}
}));
export const actionRelations = relations(actions, ({ many, one }) => ({
user: one(users, {
fields: [sql<string>`${actions.ownerId}::uuid`.as("ownerId")],
references: [users.id],
})
service: one(services, {
fields: [actions.ownerId],
references: [sql<string>`${services.id}::text`.as("id")]
}
}));
where an action's ownerId is text so that it can refer to multiple different owner types regardless of their primary key type.
1 Reply
Villager
Villager12mo ago
@Ramazan I'm running into this as well. Were you able to find a solution?
Want results from more Discord servers?
Add your server