I have an Entities:
Company:
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
@Entity()
export class Company {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
}
Unit:
import { Company } from '../../companies/entities/company.entity';
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne } from 'typeorm';
@Entity()
export class Unit {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@Column()
address: string;
@ManyToOne((type) => Company)
company: Company;
}
And DTOs for them:
export class CompanyCreateDto {
name: string
}
export class UnitCreateDto {
name: string
address: string
company: { id: number }
}
This request works fine, and creates a relation.
POST http://localhost:3000/api/units
content-type: application/json
{
"name": "Unit name 12",
"address": "city, street, house, office 234",
"company": {"id": 1}
}
If I remove "name" or "address", I get an error (which is fine):
ERROR [ExceptionsHandler] null value in column "name" of relation "unit" violates not-null constraint
But if I remove "company" from request, the entity will be saved with company: null
.
I can't understand why is that?