Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch is not updating foreign reference #62

Open
ChinaeduO opened this issue Jul 5, 2024 · 2 comments
Open

Patch is not updating foreign reference #62

ChinaeduO opened this issue Jul 5, 2024 · 2 comments

Comments

@ChinaeduO
Copy link

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
}

@kater1naa
Copy link

I am facing the exact same problem

@GabrielArsenio
Copy link

You need to remove the groupId prop from everything and use only group prop

Example:

@Entity('pricetypes')
export class PriceType extends BaseEntity {
  @ManyToOne(() => Group, group => group.pricetypes)
  @JoinColumn({ name: 'group_id', referencedColumnName: 'id' })
  group: Group;

  // ...more attributes
}
curl -X 'PATCH' \
/
  'http://localhost:3000/pricetype/2' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
  "group": { "id" : 3 }
}'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants