Drizzle ORM用户关注Schema报错:无法推断users.following关系
解决Drizzle-ORM用户关注关系Schema的推断错误
我正在开发支持用户互关的全栈社交媒体项目,使用PostgreSQL数据库,对drizzle-orm和SQL数据库比较陌生。将Schema推送到Neon时无报错,但运行drizzle-kit studio时抛出以下错误:
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
第一个Schema
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", }), }));
第二个Schema
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", }), }));
修正后的正确Schema
问题核心是关系定义时relationName不匹配,且缺少明确的字段映射,第二个Schema还存在变量引用错误。下面是修正后的单关联表写法:
import { pgTable, text, varchar, integer, primaryKey, sql } from "drizzle-orm/pg-core"; import { relations } from "drizzle-orm"; 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, ({ many }) => ({ // 用户关注的列表:关联follow表的userId字段 following: many(follow, { relationName: "user_following", fields: [users.id], references: [follow.userId], }), // 用户的粉丝列表:关联follow表的followingId字段 followers: many(follow, { relationName: "user_followers", fields: [users.id], references: [follow.followingId], }), })); 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: "user_following", }), // 被关注的用户 followedUser: one(users, { fields: [follow.followingId], references: [users.id], relationName: "user_followers", }), }));
关键修正点
- 只保留一张
follow关联表,用户关注和被关注是同一关系的双向体现,无需拆分两张表 - 在
usersRelations的many关系中,明确指定fields(当前表的关联字段)和references(关联表的对应字段),让Drizzle能准确推断关联逻辑 - 确保关系两端的
relationName完全一致(比如user_following同时用于用户的following列表和follow表的user关联) - 第二个尝试的Schema中存在变量错误(如
followingRelations里引用未定义的follow变量,应改为following),这类错误也会导致关系推断失败
内容的提问来源于stack exchange,提问作者Ujen Basi
相关产品推荐
相关产品推荐

