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

Can't patch entity to insert OneToMany relationship #780

Open
GustavoContreiras opened this issue Apr 20, 2022 · 1 comment
Open

Can't patch entity to insert OneToMany relationship #780

GustavoContreiras opened this issue Apr 20, 2022 · 1 comment

Comments

@GustavoContreiras
Copy link

GustavoContreiras commented Apr 20, 2022

When I try to patch an entity that has an one to many relationship, it fails.

My main entity, event:

import { ApiProperty } from '@nestjs/swagger'
import { Column, Entity, JoinColumn, OneToMany, OneToOne, PrimaryGeneratedColumn } from 'typeorm'
import { EventUnit } from './event-unit.entity'
import { CrudValidationGroups } from '@nestjsx/crud'
import { IsEmpty } from 'class-validator'

const { CREATE, UPDATE } = CrudValidationGroups

@Entity('evento')
export class Event {
  @PrimaryGeneratedColumn()
  id: number

  @ApiProperty({ type: () => EventUnit })
  @OneToMany(() => EventUnit, (eventUnit: EventUnit) => eventUnit.evento, {
    persistence: true,
    cascade: ['insert', 'update'],
  })
  eventUnits: EventUnit[]
}

My relationed entity, event-unit:

import { ApiProperty } from '@nestjs/swagger'
import { Column, Entity, JoinColumn, OneToOne, PrimaryGeneratedColumn } from 'typeorm'
import { Event } from './event.entity'
import { Unit } from './unit.entity'

@Entity('evento_unidade')
export class EventUnit {
  @PrimaryGeneratedColumn()
  id: number

  @ApiProperty({ description: 'Id do evento.' })
  @Column({ name: 'evento_id' })
  eventoId: number

  @ApiProperty({ type: () => Event, description: 'Evento de mensageria.' })
  @OneToOne(() => Event, {
    persistence: false,
  })
  @JoinColumn({ name: 'evento_id', referencedColumnName: 'id' })
  evento: Event

  @ApiProperty({ description: "Id da unidade do evento. Se 'null', está habilitado em todas." })
  @Column({ name: 'unidade_id' })
  unidadeId: number

  @ApiProperty({ type: () => Unit, description: 'Unidade do evento.' })
  @OneToOne(() => Unit, {
    persistence: false,
  })
  @JoinColumn({ name: 'unidade_id', referencedColumnName: 'id' })
  unidade: Unit
}

In this way, if I make a PATCH request with:

{
  "eventUnits": [
    {
      "eventoId": 1,
      "unidadeId": 0
    }
  ]
}

It returns "Column 'evento_id' cannot be null"

Tried almost all combinations of properties on entities and nothing solved.

SQLs generated (the first one detected the evento_id, but the last didn't):

[Nest] 11132  - 20/04/2022 18:04:15     LOG [TypeORM[9db900ae-4947-441b-b37d-805def837868]] query: SELECT `evento_unidade`.`id` AS `id`, `evento_unidade`.`evento_id` AS `evento_id` FROM `evento_unidade` `evento_unidade` WHERE ((`evento_unidade`.`evento_id` = ?)) -- PARAMETERS: [1]
[Nest] 11132  - 20/04/2022 18:04:15     LOG [TypeORM[9db900ae-4947-441b-b37d-805def837868]] query: START TRANSACTION
[Nest] 11132  - 20/04/2022 18:04:15     LOG [TypeORM[9db900ae-4947-441b-b37d-805def837868]] query: UPDATE `evento` SET `created_at` = ?, `updated_at` = ? WHERE `id` IN (?) -- PARAMETERS: ["2022-04-12T21:12:14.000Z","2022-04-12T21:12:15.000Z",1]
[Nest] 11132  - 20/04/2022 18:04:15     LOG [TypeORM[9db900ae-4947-441b-b37d-805def837868]] query: UPDATE `evento_unidade` SET `evento_id` = ? WHERE `id` = ? -- PARAMETERS: [null,1]
[Nest] 11132  - 20/04/2022 18:04:15     LOG [TypeORM[9db900ae-4947-441b-b37d-805def837868]] query failed: UPDATE `evento_unidade` SET `evento_id` = ? WHERE `id` = ? -- PARAMETERS: [null,1]
[Nest] 11132  - 20/04/2022 18:04:15     LOG [TypeORM[9db900ae-4947-441b-b37d-805def837868]] error:
[Nest] 11132  - 20/04/2022 18:04:15     LOG [TypeORM[9db900ae-4947-441b-b37d-805def837868]] Error: Column 'evento_id' cannot be null
[Nest] 11132  - 20/04/2022 18:04:15     LOG [TypeORM[9db900ae-4947-441b-b37d-805def837868]] query: ROLLBACK
@GustavoContreiras GustavoContreiras changed the title Can't patch entity with OneToMany relationship Can't patch entity OneToMany relationship Apr 20, 2022
@GustavoContreiras GustavoContreiras changed the title Can't patch entity OneToMany relationship Can't patch entity to insert OneToMany relationship Apr 20, 2022
@GustavoContreiras-Feegow

I think I found the solution: use create many with bulk

https://github.com/nestjsx/crud/wiki/Controllers#create-many-resources

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

2 participants