TypeORM STI inheritance issues

I´m trying to setup a STI inheritance hierarchy, similar to described on the official docs here, but getting the following error

TypeError: Cannot read property 'ownColumns' of undefined
    at /Users/luizrolim/workspace/nqa/src/metadata-builder/EntityMetadataBuilder.ts:320:64
    at Array.map (<anonymous>)
    at EntityMetadataBuilder.computeEntityMetadataStep1 (/Users/luizrolim/workspace/nqa/src/metadata-builder/EntityMetadataBuilder.ts:316:14)
    at /Users/luizrolim/workspace/nqa/src/metadata-builder/EntityMetadataBuilder.ts:108:45
    at Array.forEach (<anonymous>)

Here are me entities:

@ChildEntity()
export class ChildExam extends BaseExam {

}


@Entity('exm_exams')
@TableInheritance({ column: { type: 'varchar', name: 'type' } })
export abstract class BaseExam extends NQBaseEntity {

  @Column()
  public alias: string

  @Column()
  public description: string

}

import { BaseEntity, Column, PrimaryGeneratedColumn } from 'typeorm'
import { DateColumns } from '@infra/util/db/DateColumns'

export abstract class NQBaseEntity extends BaseEntity {
  @PrimaryGeneratedColumn()
  public id: number

  @Column(type => DateColumns)
  public dates: DateColumns
}

I am running at "typeorm": "^0.2.12",

Upvotes: 5

Views: 2459

Answers (2)

Benny Code
Benny Code

Reputation: 54890

I also ran into this error when trying to make Single Table Inheritance work:

(node:22416) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'ownColumns' of undefined at metadata-builder\EntityMetadataBuilder.ts:356:64

My problem was, that I only listed my child entities in the connection options but I forgot to also add my parent entity.

Here is how I solved it (using TypeORM v0.3.0-rc.19):

ParentEntity.ts

import {BaseEntity, Entity} from 'typeorm';

@Entity()
@TableInheritance({ column: { type: 'varchar', name: 'type' } })
export abstract class ParentEntity extends BaseEntity {
  // ...
}

ChildEntity.ts

import {ChildEntity} from 'typeorm';

@ChildEntity()
export class ChildEntity extends ParentEntity {
  constructor() {
    super();
  }

  // ...
}

initDatabase.ts

import 'reflect-metadata';
import {Connection, createConnection} from 'typeorm';
import {SqliteConnectionOptions} from 'typeorm/driver/sqlite/SqliteConnectionOptions';
import {PostgresConnectionOptions} from 'typeorm/driver/postgres/PostgresConnectionOptions';
import {ConnectionOptions} from 'typeorm/connection/ConnectionOptions';
import {ParentEntity, ChildEntity} from './entity/index';

function getConnectionOptions(env?: string): ConnectionOptions {
  // Here is the important part! Listing both, parent and child entity!
  const entities = [ParentEntity, ChildEntity];

  const test: SqliteConnectionOptions = {
    database: ':memory:',
    dropSchema: true,
    entities,
    logging: false,
    name: 'default',
    synchronize: true,
    type: 'sqlite',
  };

  const development: SqliteConnectionOptions = {
    database: 'test.db3',
    type: 'sqlite',
  };

  const production: PostgresConnectionOptions = {
    type: 'postgres',
    url: process.env.DATABASE_URL,
  };

  const devProd = {
    name: 'default',
    entities,
    logging: false,
    migrations: [],
    subscribers: [],
    synchronize: true,
  };

  switch (env) {
    case 'production':
      return Object.assign(production, devProd);
    case 'test':
      return test;
    default:
      return Object.assign(development, devProd);
  }
}

export default function initDatabase(): Promise<Connection> {
  const options = getConnectionOptions(process.env.NODE_ENV);
  return createConnection(options);
}

In my application I now have to call await initDatabase() at first, to properly initialize the database schema.

Upvotes: 4

AndyK
AndyK

Reputation: 1

You should make class BaseExam not abstract to make it work.

Upvotes: 0

Related Questions