drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
21.44k stars 484 forks source link

[BUG]: Relations inconsistent to what defined in schema and what is generated. #2499

Open kam-st opened 2 weeks ago

kam-st commented 2 weeks ago

What version of drizzle-orm are you using?

0.31.2

What version of drizzle-kit are you using?

0.22.4

Describe the Bug

Inconsistencies of what was defined in schema vs what is generated.

Following was defined.


export const UserRoleTable = pgTable("UserRole", {
  role: text("role").unique().notNull().primaryKey(),
});

export const UserTable = pgTable("User", {
  id: uuid("id").primaryKey().defaultRandom().notNull(),
  name: text("name"),
  lastName: text("lastName"),
  email: text("email").notNull().unique(),
  emailVerified: timestamp("emailVerified", { mode: "date" }),
  image: text("image"),
  password: text("password"),
  role: text("role").notNull().default("USER"),
  isTwoFactorEnabled: boolean("isTwoFactorEnabled").default(false).notNull(),
});
export const UserRoleRelations = relations(UserRoleTable, ({ many }) => ({
  Users: many(UserTable),
}));

export const UserRelations = relations(UserTable, ({ many, one }) => ({
  Accounts: many(AccountTable),
  sessions: many(SessionTable),
  TwoFactorConfirmations: many(TwoFactorConfirmationTable),
  role: one(UserRoleTable, {
    fields: [UserTable.role],
    references: [UserRoleTable.role],
  }),
}));

Check how User relations have been generated twice for role.


export const userRelations = relations(user, ({ one, many }) => ({
  accounts: many(account),
  sessions: many(session),
  twoFactorConfirmations: many(twoFactorConfirmation),
  userRoles: many(userRole, {
    relationName: "userRole_role_user_role",
  }),
  userRole: one(userRole, {
    fields: [user.role],
    references: [userRole.role],
    relationName: "user_role_userRole_role",
  }),
}));

Expected behavior

To have consistensies in relation shema generated.

Environment & setup

No response