Skip to content

Commit

Permalink
fix(schema): allow using non-abstract root entity in STI
Browse files Browse the repository at this point in the history
Closes #874
  • Loading branch information
B4nan committed Sep 26, 2020
1 parent 2cfb145 commit 9dd3aed
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
5 changes: 4 additions & 1 deletion packages/knex/src/schema/SchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,10 @@ export class SchemaGenerator {
}

private getOrderedMetadata(): EntityMetadata[] {
const metadata = Object.values(this.metadata.getAll()).filter(meta => !meta.discriminatorValue && !meta.embeddable);
const metadata = Object.values(this.metadata.getAll()).filter(meta => {
const isRootEntity = meta.root === meta;
return isRootEntity && !meta.embeddable;
});
const calc = new CommitOrderCalculator();
metadata.forEach(meta => calc.addNode(meta.name!));
let meta = metadata.pop();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`single table inheritance in mysql non-abstract root entity 1`] = `
"create table \`person\` (\`id\` varchar not null, \`type\` text check (\`type\` in ('person', 'employee')) not null, \`number\` integer null, primary key (\`id\`));
create index \`person_type_index\` on \`person\` (\`type\`);
"
`;
34 changes: 33 additions & 1 deletion tests/single-table-inheritance.mysql.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Dictionary, MetadataDiscovery, MetadataStorage, MikroORM, ReferenceType, wrap } from '@mikro-orm/core';
import { Dictionary, Entity, MetadataDiscovery, MetadataStorage, MikroORM, PrimaryKey, Property, ReferenceType, wrap } from '@mikro-orm/core';
import { MySqlDriver } from '@mikro-orm/mysql';
import { BaseUser2, CompanyOwner2, Employee2, Manager2, Type } from './entities-sql';
import { initORMMySql, wipeDatabaseMySql } from './bootstrap';
Expand Down Expand Up @@ -165,6 +165,38 @@ describe('single table inheritance in mysql', () => {
expect(discovered.get('C').discriminatorValue).toBe('c');
});

test('non-abstract root entity', async () => {
@Entity({
discriminatorColumn: 'type',
discriminatorMap: {
person: 'Person',
employee: 'Employee',
},
})
class Person {

@PrimaryKey()
id!: string;

}

@Entity()
class Employee extends Person {

@Property()
number?: number;

}

const orm = await MikroORM.init({
entities: [Person, Employee],
type: 'sqlite',
dbName: ':memory:',
});
const sql = await orm.getSchemaGenerator().getCreateSchemaSQL(false);
expect(sql).toMatchSnapshot();
});

afterAll(async () => orm.close(true));

});

0 comments on commit 9dd3aed

Please sign in to comment.