【发布时间】:2021-05-05 02:23:25
【问题描述】:
您好,我对 NestJS 中的关系有疑问。
我试图让用户与名为 club 这样的关系:
async findOne(id: number): Promise<User> {
return this.userRepository.findOne({
where: { id: id },
relations: ['club'],
});
}
这会引发此错误:
ER_PARSE_ERROR: You have an error in your SQL syntax;
check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE `User`.`id` = 1) `distinct Alias` ORDER BY `User_id` ASC LIMIT 1' at line 1
我认为我的实体看起来是正确的,这可能是 MariaDB 的问题吗?
有我的用户实体:
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column()
@Unique('email_unique', ['email'])
email: string;
@Column()
@Unique('username_unique', ['username'])
username: string;
@Column()
password: string;
@Column({ type: 'boolean' })
active = false;
@Column()
activationToken: string;
@OneToOne(() => Club, (club) => club.user)
club: Club;
}
和俱乐部实体:
@Entity()
export class Club {
@PrimaryGeneratedColumn()
id: number;
@ManyToOne(() => League)
league: League;
@OneToOne(() => User, (user) => user.club)
user: User;
@Column()
name: string;
@Column()
location: string;
@OneToMany(() => Player, (player) => player.club)
players: Player[];
}
【问题讨论】:
-
检查您的 js 代码生成的 sql 语句。我的猜测是不正确的。
标签: javascript node.js orm nestjs