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?
In my case I forgot to add @JoinColumn at a @OneToOne relation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With