DeivoT
DeivoT
DTDrizzle Team
Created by Fuzbo on 5/26/2023 in #help
Many-to-Many Self Relation
Oh I see now what I was missing x) Thank you so muuuchhh 🙏
69 replies
DTDrizzle Team
Created by Fuzbo on 5/26/2023 in #help
Many-to-Many Self Relation
and the error is the following -> Error: There is not enough information to infer relation "user.followers"
69 replies
DTDrizzle Team
Created by Fuzbo on 5/26/2023 in #help
Many-to-Many Self Relation
export const user = pgTable("User", {
id: text("id").primaryKey(),
createdAt: timestamp("createdAt").notNull().defaultNow(),
updatedAt: timestamp("updatedAt")
});

export const userRelations = relations(user, ({ many }) => ({
followers: many(user, { relationName: "followers" }),
following: many(user, { relationName: "following" })
}));

export const userNetwork = pgTable(
"UserNetwork",
{
followerId: text("followerId")
.notNull()
.references(() => user.id),
followingId: text("followingId")
.notNull()
.references(() => user.id),
createdAt: timestamp("createdAt").notNull().defaultNow(),
updatedAt: timestamp("updatedAt")
},
(table) => {
return {
pk: primaryKey(table.followerId, table.followingId)
};
}
);

export const userNetworkRelations = relations(userNetwork, ({ one }) => ({
follower: one(user, {
fields: [userNetwork.followerId],
references: [user.id],
relationName: "followers"
}),
following: one(user, {
fields: [userNetwork.followingId],
references: [user.id],
relationName: "following"
})
}));
export const user = pgTable("User", {
id: text("id").primaryKey(),
createdAt: timestamp("createdAt").notNull().defaultNow(),
updatedAt: timestamp("updatedAt")
});

export const userRelations = relations(user, ({ many }) => ({
followers: many(user, { relationName: "followers" }),
following: many(user, { relationName: "following" })
}));

export const userNetwork = pgTable(
"UserNetwork",
{
followerId: text("followerId")
.notNull()
.references(() => user.id),
followingId: text("followingId")
.notNull()
.references(() => user.id),
createdAt: timestamp("createdAt").notNull().defaultNow(),
updatedAt: timestamp("updatedAt")
},
(table) => {
return {
pk: primaryKey(table.followerId, table.followingId)
};
}
);

export const userNetworkRelations = relations(userNetwork, ({ one }) => ({
follower: one(user, {
fields: [userNetwork.followerId],
references: [user.id],
relationName: "followers"
}),
following: one(user, {
fields: [userNetwork.followingId],
references: [user.id],
relationName: "following"
})
}));
69 replies
DTDrizzle Team
Created by Fuzbo on 5/26/2023 in #help
Many-to-Many Self Relation
Hello! - @Angelelz I came up with the same solution (just a different naming) when I had to model a many to many self relation, but Im still seeing the "There is not enough information to infer relation" error 😢 do you mind taking a look at my schema and maybe figure out what am I missing?
69 replies