Omesepelepe
Omesepelepe
DTDrizzle Team
Created by Omesepelepe on 12/18/2024 in #help
"There is not enough information to infer relation" with many to many query
Hey there, I'm encountering the following error message when trying to do a many to many query:
ERR error: There is not enough information to infer relation "locations.tags"
The query:
const locationsWithTags = (await db.query.locations.findMany({
orderBy: (locations, { asc }) => [asc(locations.name)],
with: {
tags: true,
},
}))
const locationsWithTags = (await db.query.locations.findMany({
orderBy: (locations, { asc }) => [asc(locations.name)],
with: {
tags: true,
},
}))
The schema:
export const locations = sqliteTable('locations', {
id: text()
.primaryKey()
.$defaultFn(() => nanoid()),
name: text().notNull(),
slug: text().notNull().unique(),
country: text().notNull().default('us'),
});

export const locationsRelations = relations(locations, ({ one, many }) => ({
tags: many(locationsToTags),
}));

export const tags = sqliteTable('tags', {
id: text()
.primaryKey()
.$defaultFn(() => nanoid()),
name: text({ mode: 'json' }).$type<Record<string, string>>().notNull(),
slug: text().notNull().unique(),
});

export const tagsRelations = relations(locations, ({ many }) => ({
locations: many(locationsToTags),
}));

export const locationsToTags = sqliteTable(
'locations_to_tags',
{
locationId: text('location_id')
.notNull()
.references(() => locations.id),
tagId: text('tag_id')
.notNull()
.references(() => tags.id),
},
(t) => ({
pk: primaryKey({ columns: [t.locationId, t.tagId] }),
}),
);

export const locationsToTagsRelations = relations(
locationsToTags,
({ many }) => ({
tags: many(tags),
locations: many(locations),
}),
);
export const locations = sqliteTable('locations', {
id: text()
.primaryKey()
.$defaultFn(() => nanoid()),
name: text().notNull(),
slug: text().notNull().unique(),
country: text().notNull().default('us'),
});

export const locationsRelations = relations(locations, ({ one, many }) => ({
tags: many(locationsToTags),
}));

export const tags = sqliteTable('tags', {
id: text()
.primaryKey()
.$defaultFn(() => nanoid()),
name: text({ mode: 'json' }).$type<Record<string, string>>().notNull(),
slug: text().notNull().unique(),
});

export const tagsRelations = relations(locations, ({ many }) => ({
locations: many(locationsToTags),
}));

export const locationsToTags = sqliteTable(
'locations_to_tags',
{
locationId: text('location_id')
.notNull()
.references(() => locations.id),
tagId: text('tag_id')
.notNull()
.references(() => tags.id),
},
(t) => ({
pk: primaryKey({ columns: [t.locationId, t.tagId] }),
}),
);

export const locationsToTagsRelations = relations(
locationsToTags,
({ many }) => ({
tags: many(tags),
locations: many(locations),
}),
);
Not sure what I'm doing wrong here. I tried a few variations of the findMany query and naming relations differently but nothing worked so far. Any idea what's wrong? Thanks!
3 replies