【发布时间】:2021-07-28 17:45:24
【问题描述】:
当用户注册时,我希望他发送他的帐户名,所以我将有“帐户”表参考用户实体。我正在使用 Nest.js。
我正在我的 users.service.ts 注册方法中寻找以下逻辑的替代方法:
- 按名称查找帐户
- 如果找不到帐户,请创建它
- 使用上面找到的帐户创建用户
这是我的帐户实体:
@Entity()
export class Account extends BaseEntity {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column({ unique: true })
name: string;
@Column()
@CreateDateColumn()
createdAt: Date;
}
还有我的用户实体:
@Entity()
export class User extends BaseEntity {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column({ unique: true })
email: string;
@Column()
password: string;
@ManyToOne(() => Account, (Account) => Account.name, { cascade:true })
@JoinColumn({name: 'name'})
account: Account;
}
我的 CreateUserDTO:
export class CreateUserDto {
@IsEmail()
email: string;
@IsNotEmpty()
password: string;
@IsNotEmpty()
account: string;
}
这是我尝试执行User.create(dto) 时的错误:
Type 'string' is not assignable to type 'Account | DeepPartial<Account>'.
另外,由于某种原因,User.create(dto) 返回用户数组而不是单个用户,我不明白为什么。
【问题讨论】: