TriPSs / nestjs-query

Easy CRUD for GraphQL.
https://tripss.github.io/nestjs-query/
MIT License
152 stars 43 forks source link

SetRelationOnEntity failing for many to one relationships #245

Closed coupster74 closed 4 months ago

coupster74 commented 5 months ago

Describe the bug With the release of 5.0.1 5 days ago, setting a many-to-one relationship on an entity (eg an entity has a reference to a single another entity, like company contact being part of a company) now fails.

** what's with the older 5.1.0-alphas? are they a newer code base that 5.0.1? is this a mis-type or a larger update that's pending?

Seems like we've taken some steps backwards with many-to-one relationships (I also raised an issue with not being able to clear the relationship).

Have you read the Contributing Guidelines?

Yes

To Reproduce Steps to reproduce the behavior:

model entities like:

// Company Contact
@Entity()
@ObjectType()
@FilterableRelation('company', () => Company, {
  nullable: true,
  update: { enabled: true },
  remove: { enabled: true },
})

export class CompanyContact {
  @PrimaryColumn()
  @IDField(() => ID)
  @IsEmail()
  email!: string;

  @FilterableField({ nullable: true })
  @Column({ nullable: true })
  givenName?: string;

  @FilterableField({ nullable: true })
  @Column({ nullable: true })
  surname?: string;

  @Field({ nullable: true })
  @Column({ nullable: true })
  phone?: string;

  @Field({ nullable: true })
  @Column({ nullable: true })
  title?: string;

  @ManyToOne(() => Company, (company) => company.contacts)
  @JoinColumn()
  company?: Company;
} 

// company
@Entity()
@ObjectType()
@FilterableUnPagedRelation('contacts', () => CompanyContact, {
  nullable: true,
  update: { enabled: true },
  remove: { enabled: true },
})
export class Company extends BaseEntity {
  @FilterableField()
  @Column({ nullable: false })
  name!: string;

  @OneToMany(() => CompanyContact, (contact) => contact.company)
  contacts?: CompanyContact[];
}

if you attempt to assign a company to a company contact, it now does nothing:

image

Expected behavior calling the setCompanyOnCompanyContact actually does set the relationship

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

Additional context Add any other context about the problem here.

To get it to work, I needed to revert to 4.4.0 and put the relationship helper columns back in (they didn't seem to be required for v5).

TriPSs commented 5 months ago

There was indeed a mismatch with some versions, 5.0.1 is newer than 5.1.0-alpha, let me actually release the latest version so you could test it with that.

If the issue still exist could you add this bug to one of the examples so we can check it out.

TriPSs commented 4 months ago

Closing, if still an issue you can re-open.