I'm working on a full-stack social media project allowing users to follow multiple other users. I'm facing some challenges while developing the schema for this project. I'm using the PostgreSQL database. I'm relatively new to drizzle-orm and SQL-type databases. I haven't been able to run the schema successfully. If anyone could offer some guidance or assistance, I would greatly appreciate it.
When I push the schema to neon
, It shows no error, but when I run drizzle-kit studio
It throws the error:
throw new Error(`There is not enough information to infer relation "${sourceTableTsName}.${relation.fieldName}"`);
^
Error: There is not enough information to infer relation "users.following"
Schema I have tried:
export const users = pgTable("user", {
id: text("id")
.default(sql`gen_random_uuid()`)
.primaryKey(),
name: varchar("name").notNull(),
// ...
followersCount: integer("followersCount").default(0),
followingCount: integer("followingCount").default(0),
// ...
});
export const usersRelations = relations(users, ({ one, many }) => ({
// ...
following: many(follow, {
relationName: "follow",
}),
followers: many(follow, {
relationName: "followedBy",
}),
}));
export const follow = pgTable(
"follow",
{
userId: text("userId")
.references(() => users.id)
.notNull(),
followingId: text("followingId")
.references(() => users.id)
.notNull(),
},
(table) => ({
pk: primaryKey(table.userId, table.followingId),
})
);
export const followRelations = relations(follow, ({ one }) => ({
user: one(users, {
fields: [follow.userId],
references: [users.id],
relationName: "followers",
}),
following: one(users, {
fields: [follow.followingId],
references: [users.id],
relationName: "following",
}),
}));
and:
export const users = pgTable("user", {
id: text("id")
.default(sql`gen_random_uuid()`)
.primaryKey(),
name: varchar("name").notNull(),
// ...
followersCount: integer("followersCount").default(0),
followingCount: integer("followingCount").default(0),
// ...
});
export const usersRelations = relations(users, ({ one, many }) => ({
// ...
following: many(following, {
relationName: "follow",
}),
followers: many(follower, {
relationName: "followedBy",
}),
}));
export const following = pgTable(
"following",
{
userId: text("userId")
.references(() => users.id)
.notNull(),
followingId: text("followingId")
.references(() => users.id)
.notNull(),
},
(table) => ({
pk: primaryKey(table.userId, table.followingId),
})
);
export const followingRelations = relations(following, ({ one }) => ({
user: one(users, {
fields: [follow.userId],
references: [users.id],
relationName: "followers",
}),
following: one(users, {
fields: [follow.followingId],
references: [users.id],
relationName: "following",
}),
}));
export const follower = pgTable(
"follower",
{
userId: text("userId")
.references(() => users.id)
.notNull(),
followerId: text("followerId")
.references(() => users.id)
.notNull(),
},
(table) => ({
pk: primaryKey(table.userId, table.followerId),
})
);
export const followerRelations = relations(follower, ({ one }) => ({
user: one(users, {
fields: [follow.userId],
references: [users.id],
relationName: "following",
}),
follower: one(users, {
fields: [follow.followerId],
references: [users.id],
relationName: "follower",
}),
}));