[solved] One to many relation not working

I get the error "There is not enough information to infer relation" Here's (part of) my code
// /src/lib/schemas/db/fields.ts
export const fields = pgTable('fields', {
id: serial('id').primaryKey(),
name: varchar('name')
});

export const fields_lang = pgTable('fields_lang', {
id: serial('id').primaryKey(),
id_field: integer('id_field')
.notNull()
.references(() => fields.id),
id_lang: serial('id_lang').notNull(),
label: varchar('label')
});

export const fieldsRelations = relations(fields, ({ many }) => ({
lang: many(fields_lang)
}));
// /src/lib/schemas/db/fields.ts
export const fields = pgTable('fields', {
id: serial('id').primaryKey(),
name: varchar('name')
});

export const fields_lang = pgTable('fields_lang', {
id: serial('id').primaryKey(),
id_field: integer('id_field')
.notNull()
.references(() => fields.id),
id_lang: serial('id_lang').notNull(),
label: varchar('label')
});

export const fieldsRelations = relations(fields, ({ many }) => ({
lang: many(fields_lang)
}));
// /src/lib/db/client.ts
import * as schema from '$lib/schemas/db/fields';

const pool = new Pool({
connectionString: env.DB_URL
});

export const db = drizzle(pool, { schema });
// /src/lib/db/client.ts
import * as schema from '$lib/schemas/db/fields';

const pool = new Pool({
connectionString: env.DB_URL
});

export const db = drizzle(pool, { schema });
// in some route code

const fields_rows = await db.query.fields.findMany({
where: (fields, { eq }) => eq(fields.id_form, id),
with: {
lang: {
where: (fields_lang, { eq }) => eq(fields_lang.id_field, fields.id)
}
}
});

// error: There is not enough information to infer relation
// in some route code

const fields_rows = await db.query.fields.findMany({
where: (fields, { eq }) => eq(fields.id_form, id),
with: {
lang: {
where: (fields_lang, { eq }) => eq(fields_lang.id_field, fields.id)
}
}
});

// error: There is not enough information to infer relation
I must be missing something obvious
5 Replies
tunisoft
tunisoftOP•2y ago
I'm using postgres btw
Noahh
Noahh•2y ago
It looks like you need to add a fieldsLangRelations that specifies a one relation with fields https://orm.drizzle.team/docs/rqb#one-to-many Unless it's a many-to-many, in which case you might need a join table
tunisoft
tunisoftOP•2y ago
Thank you for the idea. I will try it and see if it works
Noahh
Noahh•2y ago
right now it wouldn't know which fields in fields relate to which fields in fields_lang
tunisoft
tunisoftOP•2y ago
That makes sense. I thought it would use the references part for that. I will see how to fix it It worked perfectly 💯 Thank you very much!
Want results from more Discord servers?
Add your server