Skip to content

Commit

Permalink
fix(schema): allow 1:m properties in pivot entities
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Feb 18, 2024
1 parent ac67f74 commit c370578
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/knex/src/schema/DatabaseTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
96 changes: 96 additions & 0 deletions tests/issues/GHx11.test.ts
Original file line number Diff line number Diff line change
@@ -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<Foo>(this);

}

@Entity()
class Foo {

@PrimaryKey({
unique: true,
})
id: string = v4();

@ManyToMany({
entity: () => Bar,
pivotEntity: () => FooBar,
type: Bar,
})
bars = new Collection<Bar>(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<Baz>(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();
});
19 changes: 19 additions & 0 deletions tests/issues/__snapshots__/GHx11.test.ts.snap
Original file line number Diff line number Diff line change
@@ -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;
"
`;

0 comments on commit c370578

Please sign in to comment.