Unable to infer relation

I have the following tables and relations:
export const platforms = pgTable('platforms', {
id: serial('id').primaryKey(),
name: text('name').notNull().unique(),
});

export const platformsRelations = relations(platforms, ({ many }) => ({
founders: many(founders),
}));

export const founders = pgTable('founders', {
id: serial('id').primaryKey(),
platformId: integer('platform_id')
.references(() => platforms.id)
.notNull(),
name: text('name').notNull().unique(),
});
export const platforms = pgTable('platforms', {
id: serial('id').primaryKey(),
name: text('name').notNull().unique(),
});

export const platformsRelations = relations(platforms, ({ many }) => ({
founders: many(founders),
}));

export const founders = pgTable('founders', {
id: serial('id').primaryKey(),
platformId: integer('platform_id')
.references(() => platforms.id)
.notNull(),
name: text('name').notNull().unique(),
});
My goal is to get a single platform, and all the associated founders with this query:
export const getPlatformData = async (platformName: string) => {
const platformData = await db.query.platforms.findFirst({
where: eq(platforms.name, platformName),
with: {
founders: true,
},
});

return platformData;
};
export const getPlatformData = async (platformName: string) => {
const platformData = await db.query.platforms.findFirst({
where: eq(platforms.name, platformName),
with: {
founders: true,
},
});

return platformData;
};
But this fails with the error Internal error: Error: There is not enough information to infer relation "platforms.founders" . I'm not exactly sure what I can do to fix this.
3 Replies
janosch.hrm
janosch.hrmOP14mo ago
Also, when trying include other relations that I have a join table for, everything works as expected:
export const categories = pgTable('categories', {
id: serial('id').primaryKey(),
name: Categories('name').notNull().unique(),
});

export const platformsToCategories = pgTable(
'platforms_to_categories',
{
platformId: integer('platform_id').references(() => platforms.id),
categoryId: integer('category_id').references(() => categories.id),
},
(t) => ({
pk: primaryKey(t.platformId, t.categoryId),
})
);

export const platformsToCategoriesRelations = relations(
platformsToCategories,
({ one }) => ({
platforms: one(platforms, {
fields: [platformsToCategories.platformId],
references: [platforms.id],
}),
categories: one(categories, {
fields: [platformsToCategories.categoryId],
references: [categories.id],
}),
})
);

export const getPlatformData = async (platformName: string) => {
const platformData = await db.query.platforms.findFirst({
where: eq(platforms.name, platformName),
with: {
// This doesn't work
founders: true,
// This works
catogories: true,
},
});

return platformData;
};
export const categories = pgTable('categories', {
id: serial('id').primaryKey(),
name: Categories('name').notNull().unique(),
});

export const platformsToCategories = pgTable(
'platforms_to_categories',
{
platformId: integer('platform_id').references(() => platforms.id),
categoryId: integer('category_id').references(() => categories.id),
},
(t) => ({
pk: primaryKey(t.platformId, t.categoryId),
})
);

export const platformsToCategoriesRelations = relations(
platformsToCategories,
({ one }) => ({
platforms: one(platforms, {
fields: [platformsToCategories.platformId],
references: [platforms.id],
}),
categories: one(categories, {
fields: [platformsToCategories.categoryId],
references: [categories.id],
}),
})
);

export const getPlatformData = async (platformName: string) => {
const platformData = await db.query.platforms.findFirst({
where: eq(platforms.name, platformName),
with: {
// This doesn't work
founders: true,
// This works
catogories: true,
},
});

return platformData;
};
Angelelz
Angelelz14mo ago
You are missing the foundersRelations, that should one to platforms
janosch.hrm
janosch.hrmOP14mo ago
Ah, so I always need to create the relations both ways? I thought since I'm querying from the platform, the relation from platform to founders was enough
Want results from more Discord servers?
Add your server