-
-
Notifications
You must be signed in to change notification settings - Fork 495
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): fix extra updates for composite FKs that share a column
- Loading branch information
Showing
6 changed files
with
187 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { Entity, LoadStrategy, ManyToOne, OneToOne, PrimaryKey, Property, Ref, wrap } from '@mikro-orm/core'; | ||
import { MikroORM } from '@mikro-orm/sqlite'; | ||
import { v4 } from 'uuid'; | ||
|
||
@Entity() | ||
class Organization { | ||
|
||
@PrimaryKey({ columnType: 'uuid' }) | ||
id = v4(); | ||
|
||
} | ||
|
||
@Entity() | ||
class Project { | ||
|
||
@PrimaryKey({ columnType: 'uuid' }) | ||
id = v4(); | ||
|
||
@ManyToOne({ entity: () => Organization, ref: true, primary: true }) | ||
organization!: Ref<Organization>; | ||
|
||
@Property({ length: 255 }) | ||
name!: string; | ||
|
||
@OneToOne({ | ||
entity: () => ProjectUpdate, | ||
mappedBy: 'project', | ||
ref: true, | ||
}) | ||
projectUpdate?: Ref<ProjectUpdate>; | ||
|
||
} | ||
|
||
@Entity() | ||
class ProjectUpdate { | ||
|
||
@PrimaryKey({ columnType: 'uuid' }) | ||
id = v4(); | ||
|
||
@ManyToOne({ entity: () => Organization, ref: true, primary: true }) | ||
organization!: Ref<Organization>; | ||
|
||
@OneToOne({ | ||
entity: () => Project, | ||
ref: true, | ||
joinColumns: ['project_id', 'organization_id'], | ||
}) | ||
project!: Ref<Project>; | ||
|
||
} | ||
|
||
let orm: MikroORM; | ||
let org: Organization; | ||
let project: Project; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
dbName: ':memory:', | ||
entities: [Project, Organization, ProjectUpdate], | ||
}); | ||
|
||
await orm.schema.refreshDatabase(); | ||
|
||
org = new Organization(); | ||
project = orm.em.create(Project, { | ||
organization: org, | ||
name: 'init', | ||
}); | ||
|
||
orm.em.create(ProjectUpdate, { | ||
organization: org.id, | ||
project, | ||
}); | ||
|
||
await orm.em.flush(); | ||
orm.em.clear(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await orm.close(); | ||
}); | ||
|
||
test('extra updates with 1:1 relations (joined)', async () => { | ||
const result = await orm.em.findOneOrFail(Project, { id: project.id, organization: org.id }, { | ||
populate: ['projectUpdate'], | ||
strategy: LoadStrategy.JOINED, | ||
}); | ||
|
||
expect(wrap(result.projectUpdate!.$).isTouched()).toBe(false); | ||
expect(wrap(result).isTouched()).toBe(false); | ||
|
||
orm.em.getUnitOfWork().computeChangeSets(); | ||
expect(orm.em.getUnitOfWork().getChangeSets()).toHaveLength(0); | ||
}); | ||
|
||
test('extra updates with 1:1 relations (select-in)', async () => { | ||
const result = await orm.em.findOneOrFail(Project, { id: project.id, organization: org.id }, { | ||
populate: ['projectUpdate'], | ||
strategy: LoadStrategy.SELECT_IN, | ||
}); | ||
|
||
expect(wrap(result.projectUpdate!.$).isTouched()).toBe(false); | ||
expect(wrap(result).isTouched()).toBe(false); | ||
|
||
orm.em.getUnitOfWork().computeChangeSets(); | ||
expect(orm.em.getUnitOfWork().getChangeSets()).toHaveLength(0); | ||
}); |