© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•2y ago•
9 replies
p o h a

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],
   }),
...
}))
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

How to reference composite key in foreign key
Drizzle TeamDTDrizzle Team / help
2y ago
foreign key
Drizzle TeamDTDrizzle Team / help
3y ago
Drizzle-kit keeps recreating foreign key constraints
Drizzle TeamDTDrizzle Team / help
8mo ago
Foreign Key Reference to auth Schema Not Generated in Code
Drizzle TeamDTDrizzle Team / help
3y ago