Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Referencing ClassTableChild build table error #159

Closed
jmai00 opened this issue Dec 29, 2016 · 3 comments
Closed

Referencing ClassTableChild build table error #159

jmai00 opened this issue Dec 29, 2016 · 3 comments
Labels

Comments

@jmai00
Copy link
Contributor

jmai00 commented Dec 29, 2016

I get a schema creation error when I put a OneToOne or OneToMany column that references a ClassTableChild.

Example Code is below:

import {Table, Column, PrimaryGeneratedColumn, TableInheritance, DiscriminatorColumn} from "../src/index";

@Table()
@TableInheritance("class-table")
@DiscriminatorColumn({ name: "type", type: "string"})
export class PersonA {
    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    firstName: string;

    @Column()
    lastName: string;
}
import {ClassTableChild, Column} from "../src/index";
import {Person} from "./Person";

@ClassTableChild()
export class Employee extends Person {
    @Column()
    salary: number;
}
import {Table, OneToOne, JoinColumn, PrimaryGeneratedColumn} from "../src/index";
import {Employee} from "./Employee";

@Table()
export class Department {

    @PrimaryGeneratedColumn()
    id: number;

    @OneToOne( (type) => Employee, { cascadeInsert: true, nullable: false})
    @JoinColumn()
    manager: Employee;
}
import {Table, JoinColumn, Column, PrimaryGeneratedColumn} from "../src/index";
import {OneToOne} from "../src/decorator/relations/OneToOne";
import {Employee} from "./Employee";

@Table()
export class Department {
    @PrimaryGeneratedColumn()
    id: number;
    
    @Column()
    name: string;

    @OneToOne( (type) => Employee, { cascadeInsert: true, nullable: false})
    @JoinColumn()
    manager: Employee;
}
require("reflect-metadata");
import {createConnection, ConnectionOptions, Connection} from "./src/index";
import {Person} from "./entities/Person";
import {Department} from "./entities/Department";
import {Employee} from "./entities/Employee";

let dbConnection = {
    type: "mysql",
    host: "localhost",
    port: 3306,
    username: "root",
    password: "root",
    database: "test_dev",
};

createConnection({
    driver: dbConnection,
    entities: [
        Person, Employee, Department,
    ],
    dropSchemaOnConnection: true,
    autoSchemaSync: true,
} as ConnectionOptions).then( async (connection: Connection) => {
    let x = connection.entityManager;

    let department = new Department();
    department.name = "Software";

    let employee = new Employee();
    employee.firstName = "Hello";
    employee.lastName = "World";
    employee.salary = 1;
   
    department.manager = employee;

    await x.persist(department);

    console.log("Done!");
}).catch( (error) => {
    console.log(`Error: ${error.message}`);
});

Error:

Cannot read property 'propertyType' of undefined

I traced this back to line 343 of EntityMetadataBuilder.ts where it's looking for the inverseSideColumn which is undefined in the relation.relation.joinColumn.referencedColumn. I assume the referenceColumn is supposedly the created Id column in the classtablechild

If I add a PrimaryGeneratedColumn into Employee Class, this error goes away.

@pleerock pleerock added the bug label Jan 2, 2017
@pleerock
Copy link
Member

pleerock commented Jan 2, 2017

Yes, it was a bug and I have fixed that and released in 0.0.7-alpha.3. Do npm i typeorm@0.0.7-alpha.3

@jmai00
Copy link
Contributor Author

jmai00 commented Jan 3, 2017

There is another issue which wasn't checked. After persisting, the classtablechild object (manager, Employee) doesn't get populate w/ the new database id. The department object does get the populated database id.

pleerock pushed a commit that referenced this issue Jan 12, 2017
@pleerock
Copy link
Member

fixed and released in 0.0.7-alpha.11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants