Can't patch entity to insert OneToMany relationship
GustavoContreiras opened this issue · 1 comments
GustavoContreiras commented
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-Feegow commented
I think I found the solution: use create many with bulk
https://github.com/nestjsx/crud/wiki/Controllers#create-many-resources