forked from typeorm/typeorm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: composite key save issue (typeorm#10672)
* chore(TypeORM): Create test case to uncover TypeORM composite key save issue * chore(TypeORM): Revert package.json test script alteration * fix the issue * fixed regression in 1551 * fixed test to prevent issues with auto increment when inserting rows in a different order * fixed test to prevent issues with auto increment when inserting rows in a different order --------- Co-authored-by: Umed Khudoiberdiev <[email protected]>
- Loading branch information
Showing
9 changed files
with
248 additions
and
60 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,109 @@ | ||
import "reflect-metadata" | ||
import { | ||
closeTestingConnections, | ||
createTestingConnections, | ||
reloadTestingDatabases, | ||
} from "../../utils/test-utils" | ||
import { DataSource } from "../../../src/data-source/DataSource" | ||
import { Policy } from "./entity/Policy" | ||
import { Group } from "./entity/Group" | ||
import { PolicyGroup } from "./entity/PolicyGroup" | ||
|
||
describe("other issues > composite keys doesn't work as expected in 0.3 compared to 0.2", () => { | ||
let connections: DataSource[] | ||
before( | ||
async () => | ||
(connections = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
})), | ||
) | ||
beforeEach(() => reloadTestingDatabases(connections)) | ||
after(() => closeTestingConnections(connections)) | ||
|
||
it("should properly save new relation items", () => | ||
Promise.all( | ||
connections.map(async function (connection) { | ||
const group1 = new Group() | ||
group1.id = 1 | ||
const group2 = new Group() | ||
group2.id = 2 | ||
|
||
const policy1 = new Policy() | ||
policy1.id = 1 | ||
const policy2 = new Policy() | ||
policy2.id = 2 | ||
|
||
await connection.manager.save([ | ||
group1, | ||
group2, | ||
policy1, | ||
policy2, | ||
]) | ||
|
||
const policyGroup1 = new PolicyGroup() | ||
policyGroup1.groupId = group1.id | ||
policyGroup1.policyId = policy1.id | ||
|
||
await connection.manager.save([policyGroup1]) | ||
|
||
// re-load policy | ||
const loadedPolicy = await connection.manager | ||
.getRepository(Policy) | ||
.findOneOrFail({ | ||
where: { id: 1 }, | ||
loadEagerRelations: false, | ||
}) | ||
|
||
const loadedPolicyGroups = await connection.manager | ||
.getRepository(PolicyGroup) | ||
.find({ | ||
where: { | ||
policyId: loadedPolicy.id, | ||
}, | ||
loadEagerRelations: false, | ||
}) | ||
|
||
const policyGroups2 = new PolicyGroup() | ||
policyGroups2.groupId = group2.id | ||
policyGroups2.policyId = policy1.id | ||
|
||
loadedPolicy.groups = [...loadedPolicyGroups, policyGroups2] | ||
|
||
await connection.manager.save(loadedPolicy) | ||
}), | ||
)) | ||
|
||
it("should properly save new relation items - Drata", async () => { | ||
for (const connection of connections) { | ||
let policy = new Policy() | ||
policy.id = 1 | ||
|
||
const group = new Group() | ||
group.id = 1 | ||
|
||
let policyGroup = new PolicyGroup() | ||
policyGroup.policy = policy | ||
policyGroup.group = group | ||
|
||
await connection.manager.save(policy) | ||
await connection.manager.save(group) | ||
await connection.manager.save(policyGroup) | ||
|
||
/** | ||
* Query everything back out to start fresh | ||
*/ | ||
policy = await connection.manager.findOneByOrFail(Policy, {}) | ||
policyGroup = await connection.manager.findOneByOrFail( | ||
PolicyGroup, | ||
{}, | ||
) | ||
|
||
policy.groups = [policyGroup] | ||
|
||
/** | ||
* This save fails | ||
*/ | ||
await connection.manager.save(policy) | ||
} | ||
}) | ||
}) |
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,15 @@ | ||
import { OneToMany, PrimaryColumn } from "../../../../src" | ||
import { Entity } from "../../../../src/decorator/entity/Entity" | ||
import { PolicyGroup } from "./PolicyGroup" | ||
|
||
@Entity() | ||
export class Group { | ||
@PrimaryColumn() | ||
id: number | ||
|
||
@OneToMany(() => PolicyGroup, (policyGroup) => policyGroup.policy) | ||
groups: PolicyGroup[] | ||
|
||
@OneToMany(() => PolicyGroup, (policyGroup) => policyGroup.group) | ||
policies: PolicyGroup[] | ||
} |
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,15 @@ | ||
import { Entity } from "../../../../src/decorator/entity/Entity" | ||
import { OneToMany, PrimaryColumn } from "../../../../src/index" | ||
import { PolicyGroup } from "./PolicyGroup" | ||
|
||
@Entity() | ||
export class Policy { | ||
@PrimaryColumn() | ||
id: number | ||
|
||
@OneToMany(() => PolicyGroup, (policyGroup) => policyGroup.policy) | ||
groups: PolicyGroup[] | ||
|
||
@OneToMany(() => PolicyGroup, (policyGroup) => policyGroup.group) | ||
policies: PolicyGroup[] | ||
} |
Oops, something went wrong.