many to many relation problems

I'm using planetscale and can't seem to figure out what is wrong with this relation. There's no autocompletion for with when using the RQB and trying to access the table manually throws an referenced table error

// schema
export const organizations = mysqlTable("organizations", {
  id: varchar("id", { length: 36 }).notNull().primaryKey(),
  name: varchar("name", { length: 255 }).notNull(),
  handle: varchar("handle", { length: 255 }).notNull().unique(),
  plan: mysqlEnum("plan", ["free", "scaler", "enterprise", "custom"])
    .notNull()
    .default("free"),
  ownerId: varchar("admin_id", { length: 36 }).notNull().unique(),
  avatarURL: varchar("avatar", { length: 255 }),
  createdAt: timestamp("created_at", {
    fsp: 5,
  }).defaultNow(),
});

export const organizationToUsers = mysqlTable(
  "organization_to_users",
  {
    organizationId: varchar("organization_id", { length: 36 }).notNull(),
    userId: varchar("user_id", { length: 36 }).notNull(),
  },
  (t) => ({
    pk: primaryKey(t.organizationId, t.userId),
  })
);

/**
 * ========== MANY-TO-MANY relations for organizations and users ==========
 */
export const organizationRelation = relations(
  organizations,
  ({ many, one }) => ({
    owner: one(users, {
      fields: [organizations.ownerId],
      references: [users.id],
    }),
    members: many(organizationToUsers),
  })
);

export const usersRelation = relations(users, ({ many }) => ({
  organizationToUsers: many(organizationToUsers),
}));

export const organizationToUsersRelation = relations(
  organizationToUsers,
  ({ one }) => ({
    organization: one(organizations, {
      fields: [organizationToUsers.organizationId],
      references: [organizations.id],
    }),
    user: one(users, {
      fields: [organizationToUsers.userId],
      references: [users.id],
    }),
  })
);
Was this page helpful?