如何解决毛毛雨ORM MySQL关系类型错误

jchrr9hc  于 5个月前  发布在  Mysql
关注(0)|答案(1)|浏览(45)

我试图定义一个多对多的关系之间的员工和优势表。
我得到的是关于“强度关系”定义的错误。
类型“any[]"不可分配给类型”Record<string,Relation“。类型”any[string]“中缺少类型”string“的索引签名。ts(2322)
任何见解将不胜感激。我想查询员工,并在查询中获得他们的优势(标题,而不是id)。
下面是表格定义。

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),
}));

字符串
这个定义给出了错误

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],
  }),
]));


下面是一个查询,我想使用employee_strength表中的order列按顺序获取员工及其strength.titles数组。

const results = await this.db.query.employees.findMany({
      with: {
        employeesToStrengths: true,
      },
    });


提前感谢任何见解。

tquggr8v

tquggr8v1#

下面是正确的语法

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],
    }),
  }),
);

字符串
感谢你的评分

相关问题