How to define schemas for friendship table

Just wondering whether or not i should define friendshipsRelations table. Relation: A user can have many friends
export const usersRelations = relations(users, ({ many }) => ({
friendships: many(friendships),
}));

// Friends table
export const friendships = mysqlTable(
'friends',
{
userId: int('user_id').notNull(),
friendId: int('friend_id').notNull(),
},
(t) => {
return {
pk: primaryKey(t.userId, t.friendId),
};
},
);
// Define relationships
export const friendshipsRelations = relations(friendships, ({ one }) => ({
user1: one(users, {
fields: [friendships.userId],
references: [users.id],
}),
user2: one(users, {
fields: [friendships.friendId],
references: [users.id],
}),
}));
export const usersRelations = relations(users, ({ many }) => ({
friendships: many(friendships),
}));

// Friends table
export const friendships = mysqlTable(
'friends',
{
userId: int('user_id').notNull(),
friendId: int('friend_id').notNull(),
},
(t) => {
return {
pk: primaryKey(t.userId, t.friendId),
};
},
);
// Define relationships
export const friendshipsRelations = relations(friendships, ({ one }) => ({
user1: one(users, {
fields: [friendships.userId],
references: [users.id],
}),
user2: one(users, {
fields: [friendships.friendId],
references: [users.id],
}),
}));
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server