#Storing data in table with relations with TypeORM.

6 messages · Page 1 of 1 (latest)

lavish pine
#

hI!

I am trying to handle one POST request and finding the next. problem.

I have a relation between "locations" and "customers". In fact, one user has N locations.

When I sent the request from POSTMAN, in the table DB, customerId is null

Here controller code:

  @Post()
  create(
    @Body() createLocationDto: CreateLocationDto,
    @Req() request: Request,
  ) {
    const customerId = request.body['customerId'];
    return this.locationsService.create(createLocationDto, customerId);
  } ```

service:

async create(createLocationDto: CreateLocationDto, customerId: string) {
try {
const location = this.locationRepository.create(createLocationDto);
location['customerId'] = customerId;
console.log('location', location);
await this.locationRepository.save(location);
return location;
} catch (err) {
console.log(err);
}
}

DTO

@ApiProperty()
@IsUUID()
readonly customerId: string;


Entity

@ManyToOne(() => Customer)
@JoinColumn()
customer: Customer;


I hope your help, I am not being able to find the cause.

 Thank you in advance.
snow ore
#

Worth calling out that I'm still very new to NestJS, so the problem may be deeper, but that should get you going a little further I should think...

lavish pine
sonic veldt
#

if the problem is the time to save, I see two solutions:

  1. In service.create, change location.customer
  async create(createLocationDto: CreateLocationDto, customerId: string) {
     try {
       const location = this.locationRepository.create(createLocationDto);
       location['customer'] = { id: customerId };
       console.log('location', location);
       await this.locationRepository.save(location);
       return location;
     } catch (err) {
       console.log(err);
     }
   }
  1. Add customerId column to the entity, and then you would be able to access the customerId property. Beware that this depends on how the id field is generated by the typeorm because of the 'Naming strategy' )
  @ManyToOne(() => Customer)
   @JoinColumn()
   customer: Customer;
  
   @Column( { nullable: true } ) // be careful with nullable here, as it must be in accordance with what was defined in 'customer'
   customerId: string

and then

async create(createLocationDto: CreateLocationDto, customerId: string) {
     try {
       const location = this.locationRepository.create(createLocationDto);
       location['customerId'] = customerId;
       console.log('location', location);
       await this.locationRepository.save(location);
       return location;
     } catch (err) {
       console.log(err);
     }
   }
lavish pine
#

thank you for your response @sonic veldt. Finally I found the solution with this (please let me know your opinion about it)

in the service:

constructor(
    @InjectRepository(Location)
    private locationRepository: Repository<Location>,
  ) {}

  async create(createLocationDto: CreateLocationDto, customerId: string) {
    try {
      const location = this.locationRepository.create(createLocationDto);
      location.customer = customerId;
      await this.locationRepository.save(location);
    } catch (err) {
      console.log(err);
    }
  }

in the create-location.dto

  @ApiProperty()
  @IsUUID()
  readonly customerId: string;

in the controller:

  @Post()
  async create(@Body() createLocationDto: CreateLocationDto) {
    const { customerId } = createLocationDto;
    return this.locationsService.create(createLocationDto, customerId);
  }

in the entity:

 @ManyToOne(() => Customer, (customer) => customer.locations)
  @JoinColumn({ name: 'customerId' })
  customer: string;

At the moment it is working. I don't know if this approach is correct 100%.

Thank you