question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not working properly: One-to-many relation in a Embedded entity inside Single Table Inheritance

See original GitHub issue

Issue type:

[x] bug report

Database system/driver:

[x] mssql

TypeORM version:

[x] latest

Steps to reproduce or a small repository showing the problem:

git clone https://github.com/cesarve77/typeorm-bug
cd typeorm-bug
git checkout Embedded
npm i
npm run start

Entities:


@ChildEntity()
export class Guest extends Member {

    @Column()
    cars: number
}

@Entity()
@TableInheritance({column: {name: "type", type: "varchar"}})
export  class Member {

    @PrimaryGeneratedColumn()
    id: number;

    @Column(type => BlueCard)
    blueCard: BlueCard

    @Column("varchar",{length: 50})
    name: string

    @Column("varchar",{length: 50})
    surname: string

}

export class BlueCard {

    @ManyToOne(type=>BlueCardStatus, blueCardStatus=>blueCardStatus.members, {eager: true})
    status: BlueCardStatus;

    @Column("text", {nullable: true,})
    notes: Date;

    constructor(init?: Partial<BlueCard>) {
        Object.assign(this, init);
    }
}

@Entity()
export class BlueCardStatus {

    @PrimaryGeneratedColumn()
    id: number;

    @Column("varchar", {length: 50})
    status: string

    @OneToMany(type => Member, member =>  member.blueCard  && member.blueCard.status)
    members: Member[]
}


Expecting behaviour:

when:

        const guest: Guest = new Guest()
        const parent: Parent = new Parent()

        parent.lots=12
        parent.name='name1'
        parent.surname='surname1'
        parent.blueCard=new BlueCard()
        parent.blueCard.status=new BlueCardStatus()
        parent.blueCard.status.status='status 1'

        guest.cars=1
        guest.name='name2'
        guest.surname='surname2'
        guest.blueCard=new BlueCard()
        guest.blueCard.status=new BlueCardStatus()
        guest.blueCard.status.status='status 2'

        await connection.manager.save(parent.blueCard.status)
        await connection.manager.save(guest.blueCard.status)
        await connection.manager.save(parent)

Everything saved (and it is saved but not related)

and the table looks like: image

Result:

The status relation is not saved and status field in members is saves as statusId and not as blueCardStatusId

image

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
platzhershcommented, Jan 5, 2021

having the saving issue here as well, even without the table inheritance. just by having a relation inside the embedded entiy

2reactions
gelitocommented, Jun 22, 2020

Is this issue still open?? I continue having this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Insert fails for @OneToMany Hibernate mapping with ...
I'm using Java and Hibernate 3.6.4.Final. TABLE_B is a mapping table for a specific type of A entities (B) which have a OneToMany...
Read more >
The best way to map a @OneToMany relationship with JPA ...
The @ManyToOne annotation allows you to map the Foreign Key column in the child entity mapping so that the child has an entity...
Read more >
Single Table Inheritance Strategy - JPA + Hibernate - LogicBig
In this strategy, all the classes in a hierarchy are mapped to a single table. The annotation @Inheritance is used on the root...
Read more >
Mapping a Single Entity to Multiple Tables in JPA - Baeldung
The most obvious solution is to create an entity for both classes. We can see that meal_id is both the primary key and...
Read more >
Inheritance Mapping - Doctrine Object Relational Mapper (ORM)
This means that One-To-Many associations are not possible on a mapped superclass at all. Furthermore Many-To-Many associations are only possible if the mapped ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found