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.
2 Replies
Angelelz
Angelelz8mo ago
If I understood this correctly, a user can have many referal codes Meaning, the relation is one to many If it's just one, then you can use userId as the field and referralCode as the reference
laxels
laxels8mo ago
you can use userId as the field and referralCode as the reference
Duh, I didn't think this was possible since the FK is in referralCode instead of user, but it makes sense given that relations and FKs are completely separate. This works perfectly. Thank you!
Want results from more Discord servers?
Add your server