adminjs-typeorm icon indicating copy to clipboard operation
adminjs-typeorm copied to clipboard

Updating parent relation causes duplication of child relations

Open ugened47 opened this issue 3 years ago • 2 comments

With the new versions of adminjs and adminjs-typeorm I see that child relation is duplicated when I'm editing the parent.

Here are my entities:

@Entity()
class Bill extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @Column('text', { nullable: true })
  content: string;

  @Column('jsonb', { nullable: true })
  contentJson: string;

  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;

  @OneToMany(() => Document, (document) => document.bill, {
    cascade: true,
    eager: true,
  })
  documents: Document[];

  @OneToOne(() => Proposable, { nullable: true, onDelete: 'SET NULL' })
  @JoinColumn()
  proposable?: Proposable;
}


@Entity()
class Document extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @Column('text')
  content: string;

  @Column('jsonb', { nullable: true })
  contentJson: string;

  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;

  @ManyToOne(() => Bill, (bill) => bill.documents, { onDelete: 'CASCADE' })
  bill: Bill;

  @RelationId((document: Document) => document.bill)
  billId: number;

  @OneToOne(() => Proposable, { nullable: true, onDelete: 'SET NULL' })
  @JoinColumn()
  proposable?: Proposable;
}

Once editing Bill entity with adminjs I see next:

  1. Documents are duplicated on each parent edit
  2. References of the old copies are set to null
  3. Only the last set of copies has reference to parent
Screenshot 2021-07-08 at 21 33 45

ugened47 avatar Jul 08 '21 18:07 ugened47

This happens due to eager. I've marked this as a bug since we can probably filter out eager props when saving records

dziraf avatar Feb 24 '22 10:02 dziraf

@dziraf I have a simmilar problem related to eager. (typeorm + nestjs) If I enable eager in the user address, then I can't update the adressId in the user edit page.

image image

Status is successfull, but the payload looks like this:

address.id: 2
address.street: streeeeet
address.city: cityyyyy
address.postcode: 57-222
addressId: 1

after edit, the address id is still 2.

Is it also related due to the fact that there is something wrong with the eager functionality or should I create a separate issue for this?

Thanks! :)

damian-balas avatar Dec 04 '22 19:12 damian-balas