janosch.hrm
janosch.hrm
DTDrizzle Team
Created by janosch.hrm on 11/3/2023 in #help
Unable to infer relation
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
4 replies
DTDrizzle Team
Created by janosch.hrm on 11/3/2023 in #help
Unable to infer relation
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;
};
4 replies