Currently, I am utilizing nestjs, typeorm, and nestjsx/crud to build API endpoints.
The user entity is defined like so:
@Entity({
name: 'users'
})
export class User extends CommonEntity {
@Column({ length: 104, unique: true })
email: string;
.
.
.
@OneToOne(type => Role, role => role.user, { cascade: true, lazy: true })
@JoinColumn()
role: Role;
}
As for the role entity:
@Entity({
name: 'roles'
})
export class Role extends CommonEntity {
@OneToOne(type => User, user => user.role)
user: User;
@Column({ length: 52 })
name: string;
@Column({ type: 'text' })
policy: string;
}
I am looking to create a new user (with an associated role) using a single RESTful API request. How can this be achieved?
My attempts at POSTing have been unsuccessful:
- The first attempt resulted in a duplicate record error as it tried to create a new role record.
{
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aedddadcc7c0c99e9dd6d7d4eec9c3cfc7c280cdc1c3">[email protected]</a>",
"role": {
"id": "1f07f012-9391-4b5f-b6d3-574c58f4e046"
}
}
- In the second attempt, the roleId was ignored, and no changes were made to the database.
{
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c0b3b4b2a9aea7f0f3b8b9ba80a7ada1a9aceea3afad">[email protected]</a>",
"roleId": "1f07f012-9391-4b5f-b6d3-574c58f4e046"
}