Skip to content

Is it possible to do circular dependencies between tables? #396

Answered by AndriiSherman
mkieblesz asked this question in Q&A
Discussion options

You must be logged in to vote

@mkieblesz Yes!

export const users = pgTable(
  "users",
  {
    id: serial("id").primaryKey(),
    fullName: varchar("full_name", { length: 256 }),
    authOtpId: integer("auth_otp_id")
  },
  (users) => ({
    nameIdx: index("name_idx").on(users.fullName),
    fk: foreignKey({
      columns: [users.authOtpId],
      foreignColumns: [authOtps.id],
    }),
  })
);

export const authOtps = pgTable("auth_otp", {
  id: serial("id").primaryKey(),
  phone: varchar("phone", { length: 256 }),
  userId: integer("user_id").references((): AnyPgColumn => users.id),
});

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@beeirl
Comment options

@udoniyor
Comment options

@beeirl
Comment options

@udoniyor
Comment options

Answer selected by AndriiSherman
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants