2

For a better structure, I want to split a more complex table in two entitys which results in two classes. By specifying a One-to-One-Relation, I would expect from a ORM that both were provided in the same table - Cause Joins make no sense here in a 1:1 relation and only affect performance.

So I've the following entities:

@Entity()
export class A {
    @PrimaryColumn() id: number
    // Other Properties
    @OneToOne(type => B)
    b: B
}
@Entity()
export class B {
    @Colum()
    someOtherProperty: number
}

But this doesn't work, I got an error that B has no primary key. If I specify a primary key (which makes no sense), I have two tables A and B. The documentation only describes 1:1 relations with two tables.

So why they build a unnecessary second table here? I used other powerful ORMs like EntityFramework, where this is the default behavior on 1:1 relations. Additional tables are only generated there when needed (1:n, n:n relations).

Lion
  • 16,606
  • 23
  • 86
  • 148

1 Answers1

1

In TypeOrm One-To-One relations works only between 2 tables. In the example you need to use "Embedded entities" pattern.

It must looks like:

@Entity()
export class A {
    @PrimaryColumn() id: number

    @Colum(type => B)
    b: B
}

@Entity()
export class B {
    @Colum()
    someOtherProperty: number
}

here is the docs and sample: http://typeorm.io/#/embedded-entities

D.Zotov
  • 2,044
  • 2
  • 14
  • 28