Skip to content

Concrete type isn't written down in table inheritance #2544

@lev-kuznetsov

Description

@lev-kuznetsov

Issue type:

[X] question
[X] bug report

Database system/driver:

[X] postgres
[X] sqlite

TypeORM version:

[X] 0.2.7 (or put your version here)

Steps to reproduce or a small repository showing the problem:

Maybe I'm doing something wrong; when I create a polymorphic container and save, writing down the entities the type written down for each entity is the supertype not the concrete type. Here's a snippet to reproduce, I also tried postgres with the same result

import {
  ChildEntity, Column, createConnection, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn, TableInheritance
} from 'typeorm'

@Entity()
export class ContainerOfThings {
  constructor(things?: Thing[]) {
    this.things = things
  }

  @PrimaryGeneratedColumn()
  private id: number

  @OneToMany(() => Thing, thing => thing.container, {cascade: ['insert', 'remove']})
  things: Thing[]
}

@Entity()
@TableInheritance({column: {type: 'varchar', name: 'type'}})
export abstract class Thing {
  @PrimaryGeneratedColumn()
  private id: number

  @ManyToOne(() => ContainerOfThings, c => c.things)
  container: ContainerOfThings
}

@ChildEntity()
export class ParticularThing extends Thing {
  constructor(foo: string = 'foo') {
    super()
    this.foo = foo
  }

  @Column({type: 'varchar'})
  foo: string
}

describe('class inheritance', () => {
  it('should write down type', async () => {
    const connection = await createConnection({
      synchronize: true,
      type: 'sqlite',
      database: 'inheritance-test',
      logging: true,
      entities: [ContainerOfThings, Thing, ParticularThing]
    })

    await connection.transaction(async em => {
      await em.getRepository(ContainerOfThings).save(new ContainerOfThings([
        new ParticularThing('bar'), new ParticularThing('baz'), new ParticularThing()
      ]))
    })
  })
})

If you look at the logs and also inspect the database you'll see it's written down Thing as type not ParticularThing

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions