© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•3y ago
Thogn

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],
    }),
}));
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

how to define relations for friendship?
Drizzle TeamDTDrizzle Team / help
2y ago
MySQL Table Schemas
Drizzle TeamDTDrizzle Team / help
3y ago
How to define types for jsonb
Drizzle TeamDTDrizzle Team / help
3y ago
How to define schemas (any dialect) with full text search column types?
Drizzle TeamDTDrizzle Team / help
2y ago