From c37057808aca5b6a4ac190d38374405edc833762 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ad=C3=A1mek?= Date: Sun, 18 Feb 2024 23:00:08 +0100 Subject: [PATCH] fix(schema): allow 1:m properties in pivot entities --- packages/knex/src/schema/DatabaseTable.ts | 2 +- tests/issues/GHx11.test.ts | 96 +++++++++++++++++++ tests/issues/__snapshots__/GHx11.test.ts.snap | 19 ++++ 3 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 tests/issues/GHx11.test.ts create mode 100644 tests/issues/__snapshots__/GHx11.test.ts.snap diff --git a/packages/knex/src/schema/DatabaseTable.ts b/packages/knex/src/schema/DatabaseTable.ts index b08a27d65b25..1892baeadeb2 100644 --- a/packages/knex/src/schema/DatabaseTable.ts +++ b/packages/knex/src/schema/DatabaseTable.ts @@ -83,7 +83,7 @@ export class DatabaseTable { } addColumnFromProperty(prop: EntityProperty, meta: EntityMetadata, config: Configuration) { - prop.fieldNames.forEach((field, idx) => { + prop.fieldNames?.forEach((field, idx) => { const type = prop.enum ? 'enum' : prop.columnTypes[idx]; const mappedType = this.platform.getMappedType(type); diff --git a/tests/issues/GHx11.test.ts b/tests/issues/GHx11.test.ts new file mode 100644 index 000000000000..53aa12c72632 --- /dev/null +++ b/tests/issues/GHx11.test.ts @@ -0,0 +1,96 @@ +import { + Collection, + Entity, + ManyToMany, + ManyToOne, + MikroORM, + OneToMany, + PrimaryKey, + PrimaryKeyProp, +} from '@mikro-orm/sqlite'; +import { v4 } from 'uuid'; + +@Entity() +class Bar { + + @PrimaryKey({ + unique: true, + }) + id: string = v4(); + + @ManyToMany({ + entity: () => Foo, + mappedBy: o => o.bars, + }) + foos = new Collection(this); + +} + +@Entity() +class Foo { + + @PrimaryKey({ + unique: true, + }) + id: string = v4(); + + @ManyToMany({ + entity: () => Bar, + pivotEntity: () => FooBar, + type: Bar, + }) + bars = new Collection(this); + +} + +@Entity() +class FooBar { + + @ManyToOne({ + entity: () => Foo, + primary: true, + }) + foo!: Foo; + + @ManyToOne({ + entity: () => Bar, + primary: true, + }) + user!: Bar; + + @OneToMany(() => Baz, baz => baz.foobar, { nullable: true, default: null }) + bazes = new Collection(this); + + [PrimaryKeyProp]?: ['foo', 'bar']; + +} + +@Entity() +class Baz { + + @PrimaryKey({ + unique: true, + }) + id: string = v4(); + + @ManyToOne(() => FooBar) + foobar!: FooBar; + +} + +let orm: MikroORM; + +beforeAll(async () => { + orm = await MikroORM.init({ + entities: [FooBar], + dbName: ':memory:', + }); +}); + +afterAll(async () => { + await orm.close(true); +}); + +test('1:m property inside pivot entity', async () => { + await expect(orm.schema.getUpdateSchemaSQL()).resolves.toMatchSnapshot(); +}); diff --git a/tests/issues/__snapshots__/GHx11.test.ts.snap b/tests/issues/__snapshots__/GHx11.test.ts.snap new file mode 100644 index 000000000000..5bc0c89a7f17 --- /dev/null +++ b/tests/issues/__snapshots__/GHx11.test.ts.snap @@ -0,0 +1,19 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`1:m property inside pivot entity 1`] = ` +"pragma foreign_keys = off; + +create table \`bar\` (\`id\` text not null, primary key (\`id\`)); + +create table \`foo\` (\`id\` text not null, primary key (\`id\`)); + +create table \`foo_bar\` (\`foo_id\` text not null, \`user_id\` text not null, constraint \`foo_bar_foo_id_foreign\` foreign key(\`foo_id\`) references \`foo\`(\`id\`) on update cascade, constraint \`foo_bar_user_id_foreign\` foreign key(\`user_id\`) references \`bar\`(\`id\`) on update cascade, primary key (\`foo_id\`, \`user_id\`)); +create index \`foo_bar_foo_id_index\` on \`foo_bar\` (\`foo_id\`); +create index \`foo_bar_user_id_index\` on \`foo_bar\` (\`user_id\`); + +create table \`baz\` (\`id\` text not null, \`foobar_foo_id\` text not null, \`foobar_user_id\` text not null, constraint \`baz_foobar_foo_id_foobar_user_id_foreign\` foreign key(\`foobar_foo_id\`, \`foobar_user_id\`) references \`foo_bar\`(\`foo_id\`, \`user_id\`) on update cascade, primary key (\`id\`)); +create index \`baz_foobar_foo_id_foobar_user_id_index\` on \`baz\` (\`foobar_foo_id\`, \`foobar_user_id\`); + +pragma foreign_keys = on; +" +`;