These are my tables:
@Entity('personas')
export class Personas {
@PrimaryGeneratedColumn()
@IsNumber()
id: number;
@Column({type:"varchar",length:100})
@IsNotEmpty()
nombre: string;
@OneToMany(type => Contactos, contactos => contactos.idpersona, {cascade: true})
contactos : Contactos[]
}
@Entity('contactos')
export class Contactos {
@PrimaryGeneratedColumn()
@IsNumber()
id: number;
@Column()
@IsNotEmpty()
idpersona: number;
@ManyToOne(type => Personas, {
})
@JoinColumn({name: "idpersona"})
persona: string;
@Column({type:"varchar",length:100})
nombre: string;
@Column({type:"varchar",length:11})
telefono: string;
}
This is the body of the query to add the records:
{
"nombre":"TestPersona",
"contactos":[{
"nombre":"TestContacto",
"telefono": "123456789"}
]
}
This is the error: [ExceptionsHandler] Field 'idpersona' doesn't have a default value.
It is assumed that the field idPersona in Contacts, should be inserted automatically. What am I doing wrong?. From already thank you very much.
I leave the way I fix it, in case it helps someone in the future.
In personas changed:
@OneToMany(() => Contacto, (contacto) => contacto.idpersona, {
cascade: ['insert']
})
public contactos: Contacto[]`
with:
@OneToMany(type => Contactos, contactos => contactos.personas,{
cascade : true,
})
public contactos: Contactos[];`
In contactos changed:
@Column()
@IsNotEmpty()
idpersona: number;
@ManyToOne(type => Personas, {
})
@JoinColumn({name: "idpersona"})
persona: string;`
with:
@ManyToOne(() => Personas, personas => personas.id, {nullable: false})
@JoinColumn({name: "idpersona"})
personas: Personas;