Don't understand many-to-many relationships

I have a database setup where a project can have multiple members and members can belong to many projects. A many-to-many relationship. I've created a join table UserToProject which has one-to-many relationship with the said tables. The thing is for some reason it does not work and I get an error: There is not enough information to infer relation "__public__.Project.userToProject" Here is my schema code:
export const User = pgTable("user", {
id: uuid("id").primaryKey().defaultRandom(),
username: varchar("username", { length: 255 }).notNull(),
email: varchar("email", { length: 255 }).notNull(),
password: varchar("password", { length: 255 }).notNull(),
createdAt: timestamp("created_at").defaultNow(),
})

export const UserRelations = relations(User, ({ many }) => ({
userToProject: many(UserToProject, { relationName: "projects" }),
}))

export const Project = pgTable("project", {
id: uuid("id").primaryKey().defaultRandom(),
name: varchar("name", { length: 255 }).notNull(),
website: jsonb("website"),
createdAt: timestamp("created_at").defaultNow(),
})

export const ProjectRelations = relations(Project, ({ many }) => ({
userToProject: many(UserToProject, { relationName: "members" }),
}))

export const UserToProject = pgTable(
"user_to_project",
{
userId: uuid("user_id")
.references(() => User.id)
.notNull(),
projectId: uuid("project_id")
.references(() => Project.id)
.notNull(),
},
(t) => ({
pk: primaryKey({ columns: [t.userId, t.projectId] }),
}),
)

export const UserToProjectRelations = relations(UserToProject, ({ one }) => ({
user: one(User, {
fields: [UserToProject.userId],
references: [User.id],
}),
project: one(Project, {
fields: [UserToProject.projectId],
references: [Project.id],
}),
}))
export const User = pgTable("user", {
id: uuid("id").primaryKey().defaultRandom(),
username: varchar("username", { length: 255 }).notNull(),
email: varchar("email", { length: 255 }).notNull(),
password: varchar("password", { length: 255 }).notNull(),
createdAt: timestamp("created_at").defaultNow(),
})

export const UserRelations = relations(User, ({ many }) => ({
userToProject: many(UserToProject, { relationName: "projects" }),
}))

export const Project = pgTable("project", {
id: uuid("id").primaryKey().defaultRandom(),
name: varchar("name", { length: 255 }).notNull(),
website: jsonb("website"),
createdAt: timestamp("created_at").defaultNow(),
})

export const ProjectRelations = relations(Project, ({ many }) => ({
userToProject: many(UserToProject, { relationName: "members" }),
}))

export const UserToProject = pgTable(
"user_to_project",
{
userId: uuid("user_id")
.references(() => User.id)
.notNull(),
projectId: uuid("project_id")
.references(() => Project.id)
.notNull(),
},
(t) => ({
pk: primaryKey({ columns: [t.userId, t.projectId] }),
}),
)

export const UserToProjectRelations = relations(UserToProject, ({ one }) => ({
user: one(User, {
fields: [UserToProject.userId],
references: [User.id],
}),
project: one(Project, {
fields: [UserToProject.projectId],
references: [Project.id],
}),
}))
Also a quick side question, are the docs getting worse, or am I going insane? I swear I cannot find the simplest things, even using the search.
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server