-
I'm trying to create 3 entities:
But when I generate migrations I get an extra column in the export abstract class CustomBaseEntity extends BaseEntity {
@PrimaryKey({type: types.integer})
id!: number;
} @Entity()
export class EntityA extends CustomBaseEntity {
@ManyToMany({
entity: () => EntityB,
inversedBy: (entityB: EntityB) => entityB.entityAs,
pivotEntity: () => PivotAB,
})
entityBs: Collection<EntityB> = new Collection<EntityB>(this);
} @Entity()
export class EntityB extends CustomBaseEntity {
@ManyToMany({
entity: () => EntityA,
mappedBy: (entityA: EntityA) => entityA.entityBs,
pivotEntity: () => PivotAB,
})
entityAs: Collection<EntityA> = new Collection<EntityA>(this);
} @Entity()
@Unique({properties: ["entityA", "entityB"]})
@Index({properties: ["entityA"]})
@Index({properties: ["entityB"]})
export class PivotAB extends CustomBaseEntity {
@ManyToOne({entity: () => EntityA, deleteRule: "cascade", updateRule: "cascade"})
entityA!: EntityA;
@ManyToOne({entity: () => EntityB, deleteRule: "cascade", updateRule: "cascade"})
entityB!: EntityB;
@ManyToMany({
entity: () => EntityC,
inversedBy: (entityC: EntityC) => entityC.pivotABs,
pivotEntity: () => PivotABC,
})
entityCs: Collection<EntityC> = new Collection<EntityC>(this);
} @Entity()
export class EntityC extends CustomBaseEntity {
@ManyToMany({
entity: () => PivotAB,
mappedBy: (pivotAB: PivotAB) => pivotAB.entityCs,
pivotEntity: () => PivotABC,
})
pivotABs: Collection<PivotAB> = new Collection<PivotAB>(this);
} @Entity()
@Unique({properties: ["pivotAB", "entityC"]})
@Index({properties: ["pivotAB"]})
@Index({properties: ["entityC"]})
export class PivotABC extends CustomBaseEntity {
@ManyToOne({entity: () => PivotAB, deleteRule: "cascade", updateRule: "cascade"})
pivotAB!: PivotAB;
@ManyToOne({entity: () => EntityC, deleteRule: "cascade", updateRule: "cascade"})
entityC!: EntityC;
} And the generated migration has the following line: this.addSql('create table "pivot_ab" ("id" serial primary key, "entity_a_id" int not null, "entity_b_id" int not null, "entity_cs" int not null);'); Not the extra column Here's the full migration: export class Migration20240225073523 extends Migration {
async up(): Promise<void> {
this.addSql('create table "entity_a" ("id" serial primary key);');
this.addSql('create table "entity_b" ("id" serial primary key);');
this.addSql('create table "entity_c" ("id" serial primary key);');
this.addSql('create table "pivot_ab" ("id" serial primary key, "entity_a_id" int not null, "entity_b_id" int not null, "entity_cs" int not null);');
this.addSql('create index "pivot_ab_entity_b_id_index" on "pivot_ab" ("entity_b_id");');
this.addSql('create index "pivot_ab_entity_a_id_index" on "pivot_ab" ("entity_a_id");');
this.addSql('alter table "pivot_ab" add constraint "pivot_ab_entity_a_id_entity_b_id_unique" unique ("entity_a_id", "entity_b_id");');
this.addSql('create table "pivot_abc" ("id" serial primary key, "pivot_ab_id" int not null, "entity_c_id" int not null);');
this.addSql('create index "pivot_abc_entity_c_id_index" on "pivot_abc" ("entity_c_id");');
this.addSql('create index "pivot_abc_pivot_ab_id_index" on "pivot_abc" ("pivot_ab_id");');
this.addSql('alter table "pivot_abc" add constraint "pivot_abc_pivot_ab_id_entity_c_id_unique" unique ("pivot_ab_id", "entity_c_id");');
this.addSql('alter table "pivot_ab" add constraint "pivot_ab_entity_a_id_foreign" foreign key ("entity_a_id") references "entity_a" ("id") on update cascade on delete cascade;');
this.addSql('alter table "pivot_ab" add constraint "pivot_ab_entity_b_id_foreign" foreign key ("entity_b_id") references "entity_b" ("id") on update cascade on delete cascade;');
this.addSql('alter table "pivot_abc" add constraint "pivot_abc_pivot_ab_id_foreign" foreign key ("pivot_ab_id") references "pivot_ab" ("id") on update cascade on delete cascade;');
this.addSql('alter table "pivot_abc" add constraint "pivot_abc_entity_c_id_foreign" foreign key ("entity_c_id") references "entity_c" ("id") on update cascade on delete cascade;');
}
async down(): Promise<void> {
this.addSql('alter table "pivot_ab" drop constraint "pivot_ab_entity_a_id_foreign";');
this.addSql('alter table "pivot_ab" drop constraint "pivot_ab_entity_b_id_foreign";');
this.addSql('alter table "pivot_abc" drop constraint "pivot_abc_entity_c_id_foreign";');
this.addSql('alter table "pivot_abc" drop constraint "pivot_abc_pivot_ab_id_foreign";');
this.addSql('drop table if exists "entity_a" cascade;');
this.addSql('drop table if exists "entity_b" cascade;');
this.addSql('drop table if exists "entity_c" cascade;');
this.addSql('drop table if exists "pivot_ab" cascade;');
this.addSql('drop table if exists "pivot_abc" cascade;');
}
} Is there something I'm missing or is this a bug? Thanks for helping. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
So you want to have a m:n relation with a pivot entity inside your other pivot entity? That's quite a crazy schema, we surely don't have any tests like that :] The idea behind pivot entities was mainly to allow extra scalar columns, and only just recently there was a report about something similar, which was fixed in 6.1.5 (but with 1:m relation inside pivot entity). What version are you on? |
Beta Was this translation helpful? Give feedback.
-
So I redesigned the schema to be more standard(EntityA and C now have an M:N relationship with that pivot table having 1:M relationship with Entity B) and now this is no longer necessary. I think the new design is better. It might still be worth supporting in case anyone else needs this for maybe a more complex schema, but my problem is solved. |
Beta Was this translation helpful? Give feedback.
-
This should help: f34e4d7 |
Beta Was this translation helpful? Give feedback.
This should help: f34e4d7