Drizzle MySQL Many to Many relations
I am trying to define a many to many relation ship between the employees and strengths tables.
I am getting the error on the employeeStrengthRelations definition.
Type 'any[]' is not assignable to type 'Record<string, Relation<any>>'.
Index signature for type 'string' is missing in type 'any[]'.ts(2322)
Any insight would be greatly appreciated. I want to query employees and get their strengths (title, not the id) in the query.
Here are the table definitions.
export const employees = mysqlTable('employees', {
id: serial('id').notNull().primaryKey(),
.....
});
export const strengths = mysqlTable('strengths', {
id: serial('id').notNull().primaryKey(),
title: varchar('title', { length: 255 }),
});
export const employee_strength = mysqlTable('employee_strength', {
id: serial('id').notNull().primaryKey(),
employee_id: bigint('employee_id', { mode: 'number' }).references(() => employees.id),
strength_id: bigint('strength_id', { mode: 'number' }).references(() => strengths.id),
order: int('order'),
});
export const employeeRelations = relations(employees, ({ many }) => ({
employeesToStrengths: many(employee_strength),
}));
export const strengthsRelations = relations(strengths, ({ many }) => ({
employeesToStrengths: many(employee_strength),
}));
This definition is the one giving the error
export const employeeStrengthRelations = relations(employee_strength, ({ one }) => ([
strength: one(strengths, {
fields: [employee_strength.strength_id],
references: [strengths.id],
}),
employee: one(employees, {
fields: [employee_strength.employee_id],
references: [employees.id],
}),
]));
Here is the query, in which I want to get the employees with an array of their strength.titles in order using the order on employee_strength table.
const results = await this.db.query.employees.findMany({
with: {
employeesToStrengths: true,
},
});
Thanks in advance for any insight.
I am getting the error on the employeeStrengthRelations definition.
Type 'any[]' is not assignable to type 'Record<string, Relation<any>>'.
Index signature for type 'string' is missing in type 'any[]'.ts(2322)
Any insight would be greatly appreciated. I want to query employees and get their strengths (title, not the id) in the query.
Here are the table definitions.
export const employees = mysqlTable('employees', {
id: serial('id').notNull().primaryKey(),
.....
});
export const strengths = mysqlTable('strengths', {
id: serial('id').notNull().primaryKey(),
title: varchar('title', { length: 255 }),
});
export const employee_strength = mysqlTable('employee_strength', {
id: serial('id').notNull().primaryKey(),
employee_id: bigint('employee_id', { mode: 'number' }).references(() => employees.id),
strength_id: bigint('strength_id', { mode: 'number' }).references(() => strengths.id),
order: int('order'),
});
export const employeeRelations = relations(employees, ({ many }) => ({
employeesToStrengths: many(employee_strength),
}));
export const strengthsRelations = relations(strengths, ({ many }) => ({
employeesToStrengths: many(employee_strength),
}));
This definition is the one giving the error
export const employeeStrengthRelations = relations(employee_strength, ({ one }) => ([
strength: one(strengths, {
fields: [employee_strength.strength_id],
references: [strengths.id],
}),
employee: one(employees, {
fields: [employee_strength.employee_id],
references: [employees.id],
}),
]));
Here is the query, in which I want to get the employees with an array of their strength.titles in order using the order on employee_strength table.
const results = await this.db.query.employees.findMany({
with: {
employeesToStrengths: true,
},
});
Thanks in advance for any insight.