© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•3y ago•
20 replies
jakeleventhal

How do you define one-to-one relations in the same table?

export const listings = pgTable(
  'Listing',
  {
    id: text('id')
      .primaryKey()
      .$defaultFn(() => sql`gen_random_uuid()::text`)
      .notNull(),
    physicalListingId: text('physicalListingId'),
  },
  (table) => ({
    listingPhysicalListingIdFkey: foreignKey({
      columns: [table.physicalListingId],
      foreignColumns: [table.id],
      name: 'Listing_physicalListingId_fkey'
    })
      .onUpdate('cascade')
      .onDelete('set null'),
    physicalListingIdKey: uniqueIndex('Listing_physicalListingId_key').on(table.physicalListingId),
  })
);
export const listings = pgTable(
  'Listing',
  {
    id: text('id')
      .primaryKey()
      .$defaultFn(() => sql`gen_random_uuid()::text`)
      .notNull(),
    physicalListingId: text('physicalListingId'),
  },
  (table) => ({
    listingPhysicalListingIdFkey: foreignKey({
      columns: [table.physicalListingId],
      foreignColumns: [table.id],
      name: 'Listing_physicalListingId_fkey'
    })
      .onUpdate('cascade')
      .onDelete('set null'),
    physicalListingIdKey: uniqueIndex('Listing_physicalListingId_key').on(table.physicalListingId),
  })
);

export const listingRelations = relations(listings, ({ many, one }) => ({
  digitalListing: one(listings),
  physicalListing: one(listings, {
    fields: [listings.physicalListingId],
    references: [listings.id]
  }),
}));
export const listingRelations = relations(listings, ({ many, one }) => ({
  digitalListing: one(listings),
  physicalListing: one(listings, {
    fields: [listings.physicalListingId],
    references: [listings.id]
  }),
}));

How would you define a
relationName
relationName
here if there is no field for the id of the digitalListing (this schema was working in prisma world)

basically i have a table of listings, and one can be linked to another

the problem is that if i want to define a relationName in the config for digitalListing, it expects fields and references
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

relations to the same table
Drizzle TeamDTDrizzle Team / help
2y ago
how to define array relations ?
Drizzle TeamDTDrizzle Team / help
3y ago
how to define relations for friendship?
Drizzle TeamDTDrizzle Team / help
2y ago
How to define type of table with all relations included?
Drizzle TeamDTDrizzle Team / help
3y ago