, поэтому я работаю над Nest JS и, как правило, я использую Typeorm, у меня есть две таблицы, соединенные с @ManyToOne, как это. Это мои сущности
Заказать сущность
import { plainToClass } from 'class-transformer';
import { Entity, Column, CreateDateColumn, ManyToOne, JoinColumn } from 'typeorm';
import { AbstractEntity } from '../../../common/abstract.entity';
import { OrderDto } from '../dtos/order/order.dto';
import { Client } from './client.entity'
import { Collaborator } from './collaborator.entity';
import { SalesChannels } from './sales_channel.entity';
@Entity({ name: 'orders' })
export class Order extends AbstractEntity {
@CreateDateColumn({type: 'timestamp', name: 'deleted_at' })
deleted_at: Date ;
@ManyToOne(() => Client, (client) => client)
@JoinColumn({ name: 'client'})
client: Client;
@ManyToOne(() => Collaborator, (collaborator) => collaborator)
@JoinColumn({ name: 'collaborator'})
collaborator: Collaborator;
@ManyToOne(() => SalesChannels, (sales_channel) => sales_channel)
@JoinColumn({ name: 'sales_channel'})
sales_channel: SalesChannels;
@Column({ nullable: false})
payment_method: string;
@Column({ nullable: true})
promotion: string;
@Column({ nullable:false})
tva_code: number;
@Column({ nullable:true})
status:string;
Клиентская сущность
import { plainToClass } from 'class-transformer';
import { Entity, Column, OneToMany } from 'typeorm';
import { AbstractEntity } from '../../../common/abstract.entity';
import { ClientDto } from '../dtos/client/client.dto';
import { Order } from './order.entity';
@Entity({ name: 'clients' })
export class Client extends AbstractEntity {
@Column({ nullable: false })
firstName: string;
@Column({ nullable: true })
lastName: string;
@Column({ unique: true, nullable: true })
phone: number;
@OneToMany(() => Order, order => order.client)
orders: Order[];
toDto() {
return plainToClass(ClientDto, this);
}
}
это мои DTO orderDto
import { ApiModelProperty } from '@nestjs/swagger';
import { Exclude, Expose, Type } from 'class-transformer';
import { IsEmail, IsNotEmpty, IsNumber, IsString } from 'class-validator';
import { AbstractDto } from '../../../../common/dto/abstract.dto';
import { ClientDto } from '../client/client.dto';
import { ClientRegisterDto } from '../client/client-register.dto';
import { CollaboratorDto } from '../collaborator/collaborator.dto';
import { CollaboratorRegisterDto } from '../collaborator/collaborator-register.dto';
import { SalechChannelDto } from '../sales_channel/salech_channel.dto';
import { SalesChannelRegisterDto } from '../sales_channel/sales_channel-register.dto';
@Exclude()
export class OrderDto extends AbstractDto {
@Expose()
@IsString()
@ApiModelProperty()
readonly id!: string;
@Expose()
@IsString()
@Type(() => ClientDto)
@ApiModelProperty({ description: 'client id' })
client: ClientRegisterDto;
@Expose()
@IsString()
@Type(() => CollaboratorDto)
@ApiModelProperty({ description: 'collaborator id' })
readonly collaborator!: CollaboratorRegisterDto;
@Expose()
@IsString()
@Type(() => SalechChannelDto)
@ApiModelProperty({ description: 'sales channel id' })
readonly sales_channel!: SalesChannelRegisterDto;
@Expose()
@IsString()
@ApiModelProperty()
readonly payment_method!: string;
@Expose()
@IsString()
@ApiModelProperty()
readonly promotion: string;
@Expose()
@IsNumber()
@IsNotEmpty()
@ApiModelProperty()
readonly tva_code: number;
@Expose()
@IsString()
@ApiModelProperty()
readonly status: string;
}
и это clientRegisterDTO
import { ApiModelProperty } from '@nestjs/swagger';
import { Exclude, Expose } from 'class-transformer';
import { IsNotEmpty, IsString, IsNumber } from 'class-validator';
import { AbstractDto } from '../../../../common/dto/abstract.dto';
@Exclude()
export class ClientRegisterDto extends AbstractDto {
@Expose()
@IsString()
@ApiModelProperty()
readonly id!: string;
}
, а это clientDTO
import { ApiModelProperty } from '@nestjs/swagger';
import { Exclude, Expose } from 'class-transformer';
import { IsNotEmpty, IsString, IsNumber } from 'class-validator';
import { AbstractDto } from '../../../../common/dto/abstract.dto';
@Exclude()
export class ClientDto extends AbstractDto {
@Expose()
@IsString()
@ApiModelProperty()
readonly id!: string;
@Expose()
@IsString()
@ApiModelProperty()
readonly firstName!: string;
@Expose()
@IsString()
@ApiModelProperty()
readonly lastName!: string;
@Expose()
@IsNumber()
@IsNotEmpty()
@ApiModelProperty()
readonly phone: string;
}
я хочу сохранить только идентификатор клиента в таблице клиента, когда я пытаюсь сохранить его в базе данных, я получаю сообщение об ошибке, как показано ниже, и то же самое относится и к другим объединенным таблицам, в чем проблема?
"message": [
{
"value": {
"id": "string"
},
"property": "client",
"children": [],
"constraints": {
"isString": "client must be a string"
}
},
{
"value": {
"id": "string"
},
"property": "collaborator",
"children": [],
"constraints": {
"isString": "collaborator must be a string"
}
},
{
"value": {
"id": "string"
},
"property": "sales_channel",
"children": [],
"constraints": {
"isString": "sales_channel must be a string"
}
}
]