I do not understand relations 😟 (many to many, users can follow and have followers)

export const usersTable = sqliteTable('users', {
id: text('id').primaryKey(),
username: text('username'),
})

export const usersRelations = relations(usersTable, ({ many }) => ({
questions: many(questionsTable),
followers: many(usersTable, { relationName: 'followers' }),
following: many(usersTable, { relationName: 'following' }),
}))

export const usersToUsersTable = sqliteTable('users_to_users', {
followedId: text('followed_id')
.notNull()
.references(() => usersTable.id),
followerId: text('follower_id')
.notNull()
.references(() => usersTable.id),
})

export const usersToUsersRelations = relations(usersToUsersTable, ({ one }) => ({
followed: one(usersTable, {
fields: [usersToUsersTable.followedId],
references: [usersTable.id],
relationName: 'followers',
}),
follower: one(usersTable, {
fields: [usersToUsersTable.followerId],
references: [usersTable.id],
relationName: 'following',
}),
}))
export const usersTable = sqliteTable('users', {
id: text('id').primaryKey(),
username: text('username'),
})

export const usersRelations = relations(usersTable, ({ many }) => ({
questions: many(questionsTable),
followers: many(usersTable, { relationName: 'followers' }),
following: many(usersTable, { relationName: 'following' }),
}))

export const usersToUsersTable = sqliteTable('users_to_users', {
followedId: text('followed_id')
.notNull()
.references(() => usersTable.id),
followerId: text('follower_id')
.notNull()
.references(() => usersTable.id),
})

export const usersToUsersRelations = relations(usersToUsersTable, ({ one }) => ({
followed: one(usersTable, {
fields: [usersToUsersTable.followedId],
references: [usersTable.id],
relationName: 'followers',
}),
follower: one(usersTable, {
fields: [usersToUsersTable.followerId],
references: [usersTable.id],
relationName: 'following',
}),
}))
this is the code I tried but I messed up the relations and the error says the following:
throw new Error(
^

Error: There is not enough information to infer relation "__public__.usersTable.followers"
at normalizeRelation (C:\dev\web\next\selowafelnoe\node_modules\drizzle-orm\relations.cjs:261:9)
at C:\dev\web\next\selowafelnoe\node_modules\@drizzle-team\studio\index.js:73:67
at Array.map (<anonymous>)
at C:\dev\web\next\selowafelnoe\node_modules\@drizzle-team\studio\index.js:72:42
at Array.map (<anonymous>)
at extractRelations (C:\dev\web\next\selowafelnoe\node_modules\@drizzle-team\studio\index.js:71:51)
at sqliteQueryEngine (C:\dev\web\next\selowafelnoe\node_modules\@drizzle-team\studio\index.js:293:16)
at queryEngineForSetup (C:\dev\web\next\selowafelnoe\node_modules\@drizzle-team\studio\index.js:716:12)
at Command.<anonymous> (C:\dev\web\next\selowafelnoe\node_modules\drizzle-kit\bin.cjs:63755:62)
throw new Error(
^

Error: There is not enough information to infer relation "__public__.usersTable.followers"
at normalizeRelation (C:\dev\web\next\selowafelnoe\node_modules\drizzle-orm\relations.cjs:261:9)
at C:\dev\web\next\selowafelnoe\node_modules\@drizzle-team\studio\index.js:73:67
at Array.map (<anonymous>)
at C:\dev\web\next\selowafelnoe\node_modules\@drizzle-team\studio\index.js:72:42
at Array.map (<anonymous>)
at extractRelations (C:\dev\web\next\selowafelnoe\node_modules\@drizzle-team\studio\index.js:71:51)
at sqliteQueryEngine (C:\dev\web\next\selowafelnoe\node_modules\@drizzle-team\studio\index.js:293:16)
at queryEngineForSetup (C:\dev\web\next\selowafelnoe\node_modules\@drizzle-team\studio\index.js:716:12)
at Command.<anonymous> (C:\dev\web\next\selowafelnoe\node_modules\drizzle-kit\bin.cjs:63755:62)
1 Reply
piscopancer
piscopancerOP•10mo ago
I realize that I need a separate table follows to store a follower (from) id and following (to) user id... but I do not understand how to solve this error
Want results from more Discord servers?
Add your server