PATCH not updating foreign reference

#839 · open · 1 comments

View on GitHub ↗

ChinaeduO

Hi all, I have a problem that the library is not updating a foreign key when calling tha API with PATCH. ### This is my entity: ``` @Entity('pricetypes') export class PriceType extends BaseEntity { @Column({ name: 'group_id' }) groupId: number; @ManyToOne(() => Group, group => group.pricetypes) @JoinColumn({ name: 'group_id', referencedColumnName: 'id' }) group: Group; // ...more attributes } ``` ### This is my referenced group entity: ``` export abstract class BaseEntity { @ApiProperty({ description: 'Unique identifier of the entity' }) @PrimaryGeneratedColumn() id: number; // ..more properties } @Entity('groups') export class Group extends BaseEntity { @Column({ type: 'varchar', length: 100 }) name: string; @Column({ type: 'text' }) note: string; @OneToMany(() => PriceType, priceType => priceType.group, {eager: true}) pricetypes: PriceType[]; } ``` ### And my DTOs: ``` export class CreatePriceTypeDto { @ApiProperty() @IsNotEmpty() @IsNumber() groupId: number; // .. more attributes } export class UpdatePriceTypeDto extends PartialType(CreatePriceTypeDto) {} ``` ### Problem When creating a `pricetype` everything works as expected, but when **updating** a `pricetype` and changing the referenced `groupId`, nothing happens, the `FK groupId` is not updated. (Any non-relational column is updated correctly): ``` curl -X 'PATCH' \ 'http://localhost:3000/pricetype/2' \ -H 'accept: application/json' \ -H 'Content-Type: application/json' \ -d '{ "groupId": 3 }' ``` Result: `groupId` is still the same. Also inside the database. ``` { "id": 2, "groupId": 2 // ... more properties } ```

Comments

antonioav-dev

Hi! I had the same problem. Despite it's not documented it works like this: ### Solution If you have such entities: ``` @Entity('groups') export class Group extends BaseEntity { @Column({ type: 'varchar', length: 100 }) name: string; @Column({ type: 'text' }) note: string; @OneToMany(() => PriceType, priceType => priceType.group, {eager: true}) pricetypes: PriceType[]; } ``` and ``` @Entity('pricetypes') export class PriceType extends BaseEntity { // ...other properties @ManyToOne(() => Group, group => group.pricetypes, {eager: true}) group: Group; } ``` You have to create your CreatePriceTypeDto like this: ``` export class CreatePriceTypeDto { // ... decorators you need group: number; // Note that the name of the property is 'group' and not 'groupId' // .. more properties } ``` and then you can create/update your entity with: `{ "group": 3 }` and of course other needed fields. ### Explanation I don't know why - because it's not explained in their docs - but if you have an entity `Foo` with a property `bar: Bar` (where Bar is another referenced entity) you can create a `CreateFooDto` (and it's derived `UpdateFooDto`) with a property `bar: number`and the library will fetch the record from the table `Bar` using the id you provided in the payload (as in `{ "bar": 1 }`) and "link" the fetched record with the `Foo` entity you're creating/updating. ### Conclusion Hope I had explained well. It worked for me. Try it and let me know! Cheers!