我正在使用 MySQL DB、TypeORM 和 ExpressJS。
我有 2 个实体:用户和客户端。它们之间存在一对一的关系。客户端有外键。
保存客户端时出现以下错误:
Cannot perform update query because update values are not defined. Call "qb.set(...)" method to specify updated values
用户实体:
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
}
客户端实体:
@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
}
auth.service 中的代码概述,我在其中保存 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)
当我从实体客户端中删除列user
时,客户端和用户将被保存,但分开保存,它们之间没有关系。但我想要他们之间建立关系。
我做错了什么?
我该如何解决这个问题?
最佳答案
就我而言,我忘记在 @OneToOne
关系中添加 @JoinColumn
。
关于mysql - 类型ORM。无法执行更新查询,因为未定义更新值。调用\"qb.set(...)\"方法指定更新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70961827/