0
@Entity()
export class Job {

  @PrimaryKey({ type: BigIntType })
  id: string;

  @OneToMany(() => JobExperienceLevel, 
   jobExperienceLevel => jobExperienceLevel.job, {cascade: Cascade.ALL], orphanRemoval: true})
  experienceLevels = new Collection<JobExperienceLevel>(this);

}


@Entity()
export class JobExperienceLevel {

  @PrimaryKey()
  @Enum({
    items: () => JobExperienceLevelType
  })
  experienceLevel: JobExperienceLevelType;

  @ManyToOne({nullable:false, primary: true, joinColumn: 'job_id'})
  job: Job;
}

export enum JobExperienceLevelType {
  ENTRY_LEVEL = 'ENTRY_LEVEL',
  JUNIOR = 'JUNIOR',
  REGULAR = 'REGULAR',
  SENIOR = 'SENIOR'
}

在某个作业实体上调用 experienceLevels.removeAll() 后,它会生成以下查询:

 delete from `job_experience_level` where `experience_level` = 'SENIOR' and `job_id` is null

数据库表“job_experience_level”仅包含复合主键(experience_level、job_id)

我已经检查过在调用 removeAll 方法之前集合中有一个实体“高级”。

我在作业实体上使用带有 persistAndFlush 的 entityrepository。

问题是这个查询是错误的,它应该填充正确的job_id。

我还尝试从 experienceLevel 属性中删除 @PrimaryKey() ,但是事务中根本没有删除查询。

4

1 回答 1

0

正如评论中所讨论的,存在孤立删除和复合键的错误。升级到 v3.6.7 来修复它。

https://github.com/mikro-orm/mikro-orm/blob/master/CHANGELOG.md#367-2020-04-16

这是一个测试用例,以确保它确实有效:

https://github.com/mikro-orm/mikro-orm/commit/94c71c89a648e03fad38a93cced7fa92bbfd7ff7#diff-595473b980e4d4384f667f60dbddde2aR1

于 2020-04-16T19:51:43.387 回答