relations help

how can i have a many-to-many relation for example a user can have multiple followers and can follow multple users
export const users = pgTable(
'users',
{
id: text('id').primaryKey().notNull(),
name: text('name'),
bio: text('bio').default("I Don''t Need A Bio, I''m A Developer"),
email: text('email'),
avatar: text('avatar').default('https://i.imgur.com/6VBx3io.png'),
bannerImage: text('bannerImage').default('https://i.imgur.com/6VBx3io.png'),
createdAt: timestamp('created_at', { precision: 3, mode: 'string' }).defaultNow().notNull(),
updatedAt: timestamp('updated_at', { precision: 3, mode: 'string' }).notNull(),
role: role('role'),
preferences: jsonb('preferences'),
auth: jsonb('auth'),
invitedBy: text('invitedBy'),
},
(table) => {
return {
emailKey: uniqueIndex('users_email_key').on(table.email),
};
},
);

export const userRelations = relations(users, ({ one, many }) => ({
invitee: one(users, {
fields: [users.invitedBy],
references: [users.id],
}),
// followers: many(users, {
// relationName: 'followers',
// }),
// following: many(users, {
// relationName: 'following',
// }),
posts: many(post),
comments: many(comment),
likes: many(like),
}));
export const users = pgTable(
'users',
{
id: text('id').primaryKey().notNull(),
name: text('name'),
bio: text('bio').default("I Don''t Need A Bio, I''m A Developer"),
email: text('email'),
avatar: text('avatar').default('https://i.imgur.com/6VBx3io.png'),
bannerImage: text('bannerImage').default('https://i.imgur.com/6VBx3io.png'),
createdAt: timestamp('created_at', { precision: 3, mode: 'string' }).defaultNow().notNull(),
updatedAt: timestamp('updated_at', { precision: 3, mode: 'string' }).notNull(),
role: role('role'),
preferences: jsonb('preferences'),
auth: jsonb('auth'),
invitedBy: text('invitedBy'),
},
(table) => {
return {
emailKey: uniqueIndex('users_email_key').on(table.email),
};
},
);

export const userRelations = relations(users, ({ one, many }) => ({
invitee: one(users, {
fields: [users.invitedBy],
references: [users.id],
}),
// followers: many(users, {
// relationName: 'followers',
// }),
// following: many(users, {
// relationName: 'following',
// }),
posts: many(post),
comments: many(comment),
likes: many(like),
}));
this seem to not work and im told to give more information about relation "user.followers"
1 Reply
Oreki
OrekiOP16mo ago
i've tried doing this but this also does not work
export const userRelations = relations(users, ({ one, many }) => ({
invitee: one(users, {
fields: [users.invitedBy],
references: [users.id],
relationName: 'invitee',
}),
followers: many(followers),
following: many(following),
posts: many(post),
comments: many(comment),
likes: many(like),
}));

export const followers = pgTable(
'user_followers',
{
userId: text('user_id')
.notNull()
.references(() => users.id),
followerId: text('follower_id')
.notNull()
.references(() => users.id),
},
(t) => ({
cpk: primaryKey(t.userId, t.followerId),
}),
);

export const following = pgTable(
'user_following',
{
userId: text('user_id')
.notNull()
.references(() => users.id),
followingId: text('following_id')
.notNull()
.references(() => users.id),
},
(t) => ({
cpk: primaryKey(t.userId, t.followingId),
}),
);

export const followersRelations = relations(followers, ({ one }) => ({
user: one(users, {
fields: [followers.userId],
references: [users.id],
relationName: 'followers',
}),
follower: one(users, {
fields: [followers.followerId],
references: [users.id],
relationName: 'follower',
}),
}));

export const followingRelations = relations(following, ({ one }) => ({
user: one(users, {
fields: [following.userId],
references: [users.id],
relationName: 'followers',
}),
following: one(users, {
fields: [following.followingId],
references: [users.id],
relationName: 'following',
}),
}));
export const userRelations = relations(users, ({ one, many }) => ({
invitee: one(users, {
fields: [users.invitedBy],
references: [users.id],
relationName: 'invitee',
}),
followers: many(followers),
following: many(following),
posts: many(post),
comments: many(comment),
likes: many(like),
}));

export const followers = pgTable(
'user_followers',
{
userId: text('user_id')
.notNull()
.references(() => users.id),
followerId: text('follower_id')
.notNull()
.references(() => users.id),
},
(t) => ({
cpk: primaryKey(t.userId, t.followerId),
}),
);

export const following = pgTable(
'user_following',
{
userId: text('user_id')
.notNull()
.references(() => users.id),
followingId: text('following_id')
.notNull()
.references(() => users.id),
},
(t) => ({
cpk: primaryKey(t.userId, t.followingId),
}),
);

export const followersRelations = relations(followers, ({ one }) => ({
user: one(users, {
fields: [followers.userId],
references: [users.id],
relationName: 'followers',
}),
follower: one(users, {
fields: [followers.followerId],
references: [users.id],
relationName: 'follower',
}),
}));

export const followingRelations = relations(following, ({ one }) => ({
user: one(users, {
fields: [following.userId],
references: [users.id],
relationName: 'followers',
}),
following: one(users, {
fields: [following.followingId],
references: [users.id],
relationName: 'following',
}),
}));
Want results from more Discord servers?
Add your server