Internal error: Error: There is not enough information to infer relation "users.contacts"

Hey can someone please help me with this error this is my schmea
export const userRole = pgEnum("userRole", ["admin", "user"]);

export const users = pgTable("users", {
id: uuid("id").defaultRandom().primaryKey(),
email: text("email").unique().notNull(),
avatar_url: text("avatar_url"),
first_name: text("first_name").notNull(),
last_name: text("last_name").notNull(),
job_title: text("job_title"),
department: text("department"),
slug: text("slug").notNull(),
about_title: text("about_title"),
about: varchar("about", { length: 100 }),
created_at: timestamp("created_at").defaultNow(),
workspace_id: uuid("workspace_id").references(() => workspaces.id, {
onDelete: "cascade",
onUpdate: "cascade"
})
});

export const usersRelations = relations(users, ({ one, many }) => ({
permission: one(permissions, {
fields: [users.email],
references: [permissions.user_email]
}),
contacts: many(contacts,{
relationName: 'users_contacts',
})
}));

export const contactType = pgEnum(....);

export const contacts = pgTable("contacts", {
id: uuid("id").defaultRandom().primaryKey(),
type: contactType("type").default("phone").notNull(),
value: text("value").notNull(),
title: text("title").notNull(),
created_at: timestamp("created_at").defaultNow(),
user_email: text("user_email")
.references(() => users.email, {
onDelete: "cascade",
onUpdate: "cascade"
})
.unique()
.notNull()
});

export const contactsRelations = relations(contacts, ({ many }) => ({
users: many(users)
}));
export const userRole = pgEnum("userRole", ["admin", "user"]);

export const users = pgTable("users", {
id: uuid("id").defaultRandom().primaryKey(),
email: text("email").unique().notNull(),
avatar_url: text("avatar_url"),
first_name: text("first_name").notNull(),
last_name: text("last_name").notNull(),
job_title: text("job_title"),
department: text("department"),
slug: text("slug").notNull(),
about_title: text("about_title"),
about: varchar("about", { length: 100 }),
created_at: timestamp("created_at").defaultNow(),
workspace_id: uuid("workspace_id").references(() => workspaces.id, {
onDelete: "cascade",
onUpdate: "cascade"
})
});

export const usersRelations = relations(users, ({ one, many }) => ({
permission: one(permissions, {
fields: [users.email],
references: [permissions.user_email]
}),
contacts: many(contacts,{
relationName: 'users_contacts',
})
}));

export const contactType = pgEnum(....);

export const contacts = pgTable("contacts", {
id: uuid("id").defaultRandom().primaryKey(),
type: contactType("type").default("phone").notNull(),
value: text("value").notNull(),
title: text("title").notNull(),
created_at: timestamp("created_at").defaultNow(),
user_email: text("user_email")
.references(() => users.email, {
onDelete: "cascade",
onUpdate: "cascade"
})
.unique()
.notNull()
});

export const contactsRelations = relations(contacts, ({ many }) => ({
users: many(users)
}));
but when i do like this
const workspaceUsers = await db.query.users.findMany({
where: eq(users.workspace_id, loggedWorkspace.data.id),
with: {
permission:true,
contacts: true
}
});
const workspaceUsers = await db.query.users.findMany({
where: eq(users.workspace_id, loggedWorkspace.data.id),
with: {
permission:true,
contacts: true
}
});
im getting this error ⨯ Internal error: Error: There is not enough information to infer relation "users.contacts" please help me !!
2 Replies
Angelelz
Angelelz10mo ago
This means that you have to define your relations on both "sides" You defined the relation from user -> contacts You also need to define from contacts -> user
sujjeee
sujjeeeOP10mo ago
Okay , Thanks for the reply 🙂
Want results from more Discord servers?
Add your server