orenmizr
orenmizr
DTDrizzle Team
Created by orenmizr on 9/4/2023 in #help
Help creating many to many (user has many followers, user has many followees)
is this the right solution ?! my first attempts was putting 2 fields on the users followers, followees and i was corrected to this code : cannot be loaded work says users relation is missing something
import { InferModel, relations, sql } from "drizzle-orm";
import { pgTable, serial, text, varchar, integer, boolean, primaryKey, timestamp } from "drizzle-orm/pg-core";

export const usersRelations = relations(users, ({ many }) => ({
followers: many(usersFollowers, {
relationName: "followers",
}),
followees: many(usersFollowers, {
relationName: "followees",
}),
}));

export const usersFollowers = pgTable(
"users_followers",
{
followerId: integer("follower_id").references(() => users.id, {
onDelete: "cascade",
}),
followeeId: integer("followee_id").references(() => users.id, {
onDelete: "cascade",
}),
},
(t) => ({
pk: primaryKey(t.followerId, t.followeeId),
})
);

export const userFollowRelations = relations(usersFollowers, ({ one }) => ({
follower: one(users, {
fields: [usersFollowers.followerId],
references: [users.id],
}),
followee: one(users, {
fields: [usersFollowers.followeeId],
references: [users.id],
}),
}));
import { InferModel, relations, sql } from "drizzle-orm";
import { pgTable, serial, text, varchar, integer, boolean, primaryKey, timestamp } from "drizzle-orm/pg-core";

export const usersRelations = relations(users, ({ many }) => ({
followers: many(usersFollowers, {
relationName: "followers",
}),
followees: many(usersFollowers, {
relationName: "followees",
}),
}));

export const usersFollowers = pgTable(
"users_followers",
{
followerId: integer("follower_id").references(() => users.id, {
onDelete: "cascade",
}),
followeeId: integer("followee_id").references(() => users.id, {
onDelete: "cascade",
}),
},
(t) => ({
pk: primaryKey(t.followerId, t.followeeId),
})
);

export const userFollowRelations = relations(usersFollowers, ({ one }) => ({
follower: one(users, {
fields: [usersFollowers.followerId],
references: [users.id],
}),
followee: one(users, {
fields: [usersFollowers.followeeId],
references: [users.id],
}),
}));
15 replies