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 listingRelations = relations(listings, ({ many, one }) => ({
  digitalListing: one(listings),
  physicalListing: one(listings, {
    fields: [listings.physicalListingId],
    references: [listings.id]
  }),
}));

How would you define a 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
Was this page helpful?