-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: return null for nullable RelationId() column (#6848)
Fix issue where attempting to cast bigints to strings also did it for null values. Fix issue where transformRelationIds() filtered out null values when it should only be done for undefined. Closes: #6815
- Loading branch information
Showing
5 changed files
with
81 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import {Entity, PrimaryGeneratedColumn} from "../../../../src"; | ||
|
||
@Entity() | ||
export class ChildEntity { | ||
@PrimaryGeneratedColumn({type: "bigint"}) | ||
id: string; | ||
} |
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, JoinColumn, OneToOne, PrimaryGeneratedColumn, RelationId} from "../../../../src"; | ||
import {ChildEntity} from "./ChildEntity"; | ||
|
||
@Entity() | ||
export class ParentEntity { | ||
@PrimaryGeneratedColumn({type: "bigint"}) | ||
id: string; | ||
|
||
@OneToOne(() => ChildEntity, {nullable: true}) | ||
@JoinColumn() | ||
child: ChildEntity|null; | ||
|
||
@RelationId((parent: ParentEntity) => parent.child) | ||
childId: string|null; | ||
} |
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,56 @@ | ||
import {expect} from "chai"; | ||
import {Connection} from "../../../src/connection/Connection"; | ||
import {EntityManager} from "../../../src/entity-manager/EntityManager"; | ||
import { | ||
createTestingConnections, | ||
closeTestingConnections, | ||
reloadTestingDatabases, | ||
} from "../../utils/test-utils"; | ||
import {ChildEntity} from "./entity/ChildEntity"; | ||
import {ParentEntity} from "./entity/ParentEntity"; | ||
|
||
describe("github issues > #6815 RelationId() on nullable relation returns 'null' string", () => { | ||
let connections: Connection[]; | ||
|
||
before(async () => connections = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
schemaCreate: true, | ||
dropSchema: true, | ||
enabledDrivers: ["cockroachdb", "mariadb", "mssql", "mysql", "postgres"] | ||
})); | ||
|
||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should return null as childId if child doesn't exist", () => Promise.all( | ||
connections.map(async connection => { | ||
const em = new EntityManager(connection); | ||
const parent = em.create(ParentEntity); | ||
await em.save(parent); | ||
|
||
const loaded = await em.findOneOrFail(ParentEntity, parent.id); | ||
expect(loaded.childId).to.be.null; | ||
}) | ||
)); | ||
|
||
it("should return string as childId if child exists", () => Promise.all( | ||
connections.map(async connection => { | ||
const em = new EntityManager(connection); | ||
const child = em.create(ChildEntity); | ||
await em.save(child); | ||
|
||
const parent = em.create(ParentEntity); | ||
parent.child = child; | ||
await em.save(parent); | ||
|
||
const loaded = await em.findOneOrFail(ParentEntity, parent.id); | ||
|
||
if (connection.name === "cockroachdb") { | ||
// CockroachDB returns id as a number. | ||
expect(loaded.childId).to.equal(child.id.toString()); | ||
} else { | ||
expect(loaded.childId).to.equal(child.id); | ||
} | ||
}) | ||
)); | ||
}); |