Foreign key reference in drizzle causes type error

In my drizzle schema, I have two tables, users and payment_history, when I try to reference the id of payment_history, it throws a type error Function implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions.. users table and payment_history table have a one to many relation where one user can have many payments. These are my schema definition: users:
export const users = pgTable("users",{
...
last_payment_id: varchar("last_payment_id").references(() => payment_history.id, { onDelete: "cascade" }),//error
...
})
export const users = pgTable("users",{
...
last_payment_id: varchar("last_payment_id").references(() => payment_history.id, { onDelete: "cascade" }),//error
...
})
payment_history:
export const payment_history = pgTable("payment_history",{
id: varchar("id").primaryKey().unique().notNull(),
...
user_id: varchar("user_id").references(() => users.id, { onDelete: "cascade" }), //fk to users.id where i get type error
})
export const payment_history = pgTable("payment_history",{
id: varchar("id").primaryKey().unique().notNull(),
...
user_id: varchar("user_id").references(() => users.id, { onDelete: "cascade" }), //fk to users.id where i get type error
})
and these are the table relations: users:
export const userRelation = relations(users, ({ one, many }) => ({
...
payment_history: many(payment_history),
...
}))
export const paymentHistoryRelations = relations(payment_history, ({ one, many }) => ({
...
last_payment: one(users, {
fields: [payment_history.user_id],
references: [users.last_payment_id],
}),
...
}))
export const userRelation = relations(users, ({ one, many }) => ({
...
payment_history: many(payment_history),
...
}))
export const paymentHistoryRelations = relations(payment_history, ({ one, many }) => ({
...
last_payment: one(users, {
fields: [payment_history.user_id],
references: [users.last_payment_id],
}),
...
}))
7 Replies
rphlmr ⚡
rphlmr ⚡4mo ago
👋 Can you try this?
references((): AnyPgColumn => xxxx
references((): AnyPgColumn => xxxx
p o h a
p o h a4mo ago
that works but...how, I tried mentioning the return type like this before:
user_id: varchar("user_id").references(():ReturnTypeOrValue<typeof users.id> => users.id, { onDelete: "cascade" }), //fk to users.id
user_id: varchar("user_id").references(():ReturnTypeOrValue<typeof users.id> => users.id, { onDelete: "cascade" }), //fk to users.id
but this gave me a circular reference type error
rphlmr ⚡
rphlmr ⚡4mo ago
Did you find this somewhere in the doc?
p o h a
p o h a4mo ago
no i just figured if the type error is because it has an implicit any type then I should mention the type
rphlmr ⚡
rphlmr ⚡4mo ago
the AnyPgColumn is a generic but not mentioned that much in the doc 😬. The reason is https://arc.net/l/quote/bicdfvte
rphlmr ⚡
rphlmr ⚡4mo ago
Nothing wrong with your code, just some TS weird issues It hates circular references, so we have to rely on a generic to break it
p o h a
p o h a4mo ago
Thanks a ton!!! Following you on twitter!
Want results from more Discord servers?
Add your server