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],
}),
})
);
// 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],
}),
})
);
4 Replies
Luxaritas
Luxaritas15mo ago
All of these exported relations wind up in the schema object passed to the drizzle constructor, correct?
Liltripple_reid
Liltripple_reidOP15mo ago
Mmm not all The organizationToUsers is not there let me change that nice it worked! thanks
West side ⁉
West side ⁉15mo ago
what did you do? just make sure to import them all?
West side ⁉
West side ⁉15mo ago
I love the internet I just had to declare relations (i.e. using the relations function https://orm.drizzle.team/docs/rqb#declaring-relations and import it in schema)
Want results from more Discord servers?
Add your server