我正在尝试建立STI继承层次结构,类似于官方文档here中所述,但是出现以下错误
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>)
这是我的实体:
@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
}
我正在“ typeorm”处运行:“ ^ 0.2.12”,
答案 0 :(得分:0)
您应该使BaseExam类不是抽象类才能使其正常工作。
答案 1 :(得分:0)
在尝试使Single Table Inheritance工作时,我也遇到了此错误:
(节点:22416)UnhandledPromiseRejectionWarning:TypeError:无法读取 在以下位置未定义的属性“ ownColumns” 元数据生成器\ EntityMetadataBuilder.ts:356:64
我的问题是,我只在connection options中列出了我的子实体,但是忘记了也添加了我的父实体。
这是我解决的方法(使用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);
}
在我的应用程序中,我现在必须首先调用await initDatabase()
,以正确初始化数据库架构。