laxels
laxels
DTDrizzle Team
Created by laxels on 1/28/2024 in #help
TypeScript error when specifying `relationName` in a one-to-one relation
Hi there! I have the following relations:
export const userRelations = relations(user, ({ one }) => ({
// TypeScript Error: Argument of type `{relationName: string}` is not assignable to parameter of type ...
referralCode: one(referralCode, { relationName: `claimedBy` })
}));

export const referralCodeRelations = relations(referralCode, ({ one }) => ({
createdBy: one(user, {
fields: [referralCode.createdById],
references: [user.id],
relationName: `createdBy`
}),
claimedBy: one(user, {
fields: [referralCode.claimedById],
references: [user.id],
relationName: `claimedBy`
})
}));
export const userRelations = relations(user, ({ one }) => ({
// TypeScript Error: Argument of type `{relationName: string}` is not assignable to parameter of type ...
referralCode: one(referralCode, { relationName: `claimedBy` })
}));

export const referralCodeRelations = relations(referralCode, ({ one }) => ({
createdBy: one(user, {
fields: [referralCode.createdById],
references: [user.id],
relationName: `createdBy`
}),
claimedBy: one(user, {
fields: [referralCode.claimedById],
references: [user.id],
relationName: `claimedBy`
})
}));
The example code shows a case where the referralCode table has 2 fields that reference the user table, so specifying relationName is required to disambiguate. However, the config object taken by the one function requires the fields and references properties, which doesn't make sense for the user table which does not have foreign key fields. For some reason, the many function does not have this problem, and specifying just relationName is fine. Please let me know if I'm doing something wrong here.
5 replies