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: allow falsey discriminator values (typeorm#6973)
allow discriminator values in Single Table Inheritance like 0, "", or `null` fixes typeorm#3891
- Loading branch information
1 parent
627a5dd
commit 127ea14
Showing
10 changed files
with
138 additions
and
5 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
11 changes: 11 additions & 0 deletions
11
...unctional/table-inheritance/single-table/non-virtual-discriminator-column/entity/Other.ts
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,11 @@ | ||
import {Column} from "../../../../../../src/decorator/columns/Column"; | ||
import {ChildEntity} from "../../../../../../src/decorator/entity/ChildEntity"; | ||
import {Person} from "./Person"; | ||
|
||
@ChildEntity("") | ||
export class Other extends Person { | ||
|
||
@Column() | ||
mood: 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
18 changes: 18 additions & 0 deletions
18
test/functional/table-inheritance/single-table/numeric-types/entity/Person.ts
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,18 @@ | ||
import {Column} from "../../../../../../src/decorator/columns/Column"; | ||
import {TableInheritance} from "../../../../../../src/decorator/entity/TableInheritance"; | ||
import {Entity} from "../../../../../../src/decorator/entity/Entity"; | ||
import {PrimaryGeneratedColumn} from "../../../../../../src/decorator/columns/PrimaryGeneratedColumn"; | ||
|
||
@Entity() | ||
@TableInheritance({ column: { name: "type", type: "int" } }) | ||
export class Person { | ||
|
||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
name: string; | ||
|
||
@Column() | ||
type: number; | ||
} |
11 changes: 11 additions & 0 deletions
11
test/functional/table-inheritance/single-table/numeric-types/entity/Student.ts
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,11 @@ | ||
import {Column} from "../../../../../../src/decorator/columns/Column"; | ||
import {ChildEntity} from "../../../../../../src/decorator/entity/ChildEntity"; | ||
import {Person} from "./Person"; | ||
|
||
@ChildEntity(0) | ||
export class Student extends Person { | ||
|
||
@Column() | ||
faculty: string; | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
test/functional/table-inheritance/single-table/numeric-types/entity/Teacher.ts
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,11 @@ | ||
import {Column} from "../../../../../../src/decorator/columns/Column"; | ||
import {ChildEntity} from "../../../../../../src/decorator/entity/ChildEntity"; | ||
import {Person} from "./Person"; | ||
|
||
@ChildEntity(1) | ||
export class Teacher extends Person { | ||
|
||
@Column() | ||
specialization: string; | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
test/functional/table-inheritance/single-table/numeric-types/numeric-types.ts
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,57 @@ | ||
import "reflect-metadata"; | ||
import {expect} from "chai"; | ||
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../../../utils/test-utils"; | ||
import {Connection} from "../../../../../src/connection/Connection"; | ||
import {Student} from "./entity/Student"; | ||
import {Teacher} from "./entity/Teacher"; | ||
import {Person} from "./entity/Person"; | ||
import {CockroachDriver} from "../../../../../src/driver/cockroachdb/CockroachDriver"; | ||
|
||
describe("table-inheritance > single-table > numeric types", () => { | ||
|
||
let connections: Connection[]; | ||
before(async () => connections = await createTestingConnections({ | ||
entities: [Person, Student, Teacher] | ||
})); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should allow numeric types for the discriminator, including 0", () => Promise.all(connections.map(async connection => { | ||
if (connection.driver instanceof CockroachDriver) { | ||
return; | ||
} | ||
|
||
// ------------------------------------------------------------------------- | ||
// Create | ||
// ------------------------------------------------------------------------- | ||
|
||
const student = new Student(); | ||
student.name = "Alice"; | ||
student.faculty = "Economics"; | ||
await connection.getRepository(Student).save(student); | ||
|
||
const teacher = new Teacher(); | ||
teacher.name = "Roger"; | ||
teacher.specialization = "Math"; | ||
await connection.getRepository(Teacher).save(teacher); | ||
|
||
// ------------------------------------------------------------------------- | ||
// Select | ||
// ------------------------------------------------------------------------- | ||
|
||
let persons = await connection.manager | ||
.createQueryBuilder(Person, "person") | ||
.getMany(); | ||
|
||
expect(persons[0].id).to.be.equal(1); | ||
expect(persons[0].type).to.be.equal(0); | ||
expect(persons[0].name).to.be.equal("Alice"); | ||
expect((persons[0] as Student).faculty).to.be.equal("Economics"); | ||
|
||
expect(persons[1].id).to.be.equal(2); | ||
expect(persons[1].type).to.be.equal(1); | ||
expect(persons[1].name).to.be.equal("Roger"); | ||
expect((persons[1] as Teacher).specialization).to.be.equal("Math"); | ||
}))); | ||
|
||
}); |