Reputation: 59
I'm using a MySQL DB, TypeORM and ExpressJS.
I have 2 entities: User and Client. There's one-to-one relationship between them. Client has foreign key.
I get following error when I save a client:
Cannot perform update query because update values are not defined. Call "qb.set(...)" method to specify updated values
User Entity:
export class User extends BaseEntity {
@PrimaryGeneratedColumn()
id: number
@Column()
role: string
@Column()
email: string
@Column()
password: string
@Column({ default: '' })
avatar: string
@Column()
firstName: string
@Column()
lastName: string
@Column()
fullName: string
@Column({ default: '' })
phone: string
@Column({ type: 'text', nullable: true })
description: string
@Column({ nullable: true })
age: number
@OneToOne(_type => Freelancer, freelancer => freelancer.user, { nullable: true })
freelancer: Freelancer
@OneToOne(_type => Client, client => client.user, { nullable: true })
client: Client
}
Client Entity:
@Entity()
export class Client extends BaseEntity {
@PrimaryGeneratedColumn()
id: number
@Column()
companyName: string
@ManyToOne(_type => Position, position => position.clients)
position: Position
@OneToOne(_type => ClientReview, clientReview => clientReview.client, { nullable: true })
@JoinColumn()
review: ClientReview
@OneToMany(_type => Project, project => project.client, { nullable: true })
projects: Project[]
@OneToOne(_type => User, user => user.client)
@JoinColumn()
user: User
}
Overview of code in auth.service, where I save client
:
const user = userRepository.create({
email,
password: hashedPassword,
role,
description,
firstName,
lastName,
fullName: `${firstName} ${lastName}`,
phone
})
const clientRepository = getRepository(Client)
const positionRepository = getRepository(Position)
const positionEntity = await positionRepository.findOne({ id: position.id })
const client = clientRepository.create({
companyName,
position: positionEntity,
user
})
await userRepository.save(user)
await clientRepository.save(client)
When I remove column user
from entity Client, Client and User are saved, but separately, without a relation between them. But I want a relation between them.
What did I do wrong?
How should I fix it?
Upvotes: 3
Views: 10276
Reputation: 176
It would appear that there are multiple causes that can trigger this error. In my case I had an entity that had only onDelete: 'CASCADE'
@ManyToOne(() => WorkOrder, (workOrder) => workOrder.recommendations, {
onDelete: 'CASCADE',
})
workOrder!: WorkOrder;
The solution was to also specify cascade: true
Final working solution:
@ManyToOne(() => WorkOrder, (workOrder) => workOrder.recommendations, {
cascade: true,
onDelete: 'CASCADE',
})
workOrder!: WorkOrder;
Note: when adding cascade: true
it's not necessary to generate a new migration
Upvotes: 0
Reputation: 232
This error is related to saving one to one relationship int the DB on typeorm.
User Entity:
@OneToOne( Freelancer, freelancer => freelancer.user )
freelancer: Freelancer
Client Entity:
@OneToOne(_type => User, user => user.client)
@JoinColumn({ name: 'client_user_id' })
user: User
Make sure this field "client_user_id" is created in the table
Upvotes: 0
Reputation: 44
I got this error when I had { cascade: false }, but the entity I was passing was malformed without "id".
After I sent the id, it solved the issue.
Upvotes: 0
Reputation: 41
In my case I found that I had a column that I mark as not able to update inside of one of my relations. So when I tried to update the main entity the cascade effects triggered the error.
@Column({ update: false })
What I did was to remove that column from the update payload because the value was not exactly the same.
Upvotes: 0
Reputation: 499
In my case the problem was the undefined DTO I was trying to save.
this.clientRepository.update(+id, updateClientDto);
and the updateClientDto was undefined so I got the same error.
Upvotes: 0
Reputation: 700
From the Github issues, it seems like a number of different things can cause this generic error message.
In my case, I found out that the problem was that the "other" object that I was trying to save onto "this" object (in your example, "other" = user
) wasn't a full object yet (I might not be using the right terminology). In my case, I had to fully retrieve user
before attaching it to client
and then saving client.
Upvotes: 0
Reputation: 142
HI you have to make the relationship using
client.user = user
before saving your client & user
Upvotes: 0