【问题标题】:ER_PARSE_ERROR in Nest jsNest js 中的 ER_PARSE_ERROR
【发布时间】: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


【解决方案1】:

您缺少JoinColumn 注释 - 这可能是错误生成 sql 的原因。尝试在拥有方定义JoinColumn,例如

export class User {
  // ...
  @OneToOne(() => Club, (club) => club.user)
  @JoinColumn()
  club: Club;
}

【讨论】:

    猜你喜欢
    • 2019-05-08
    • 2022-10-14
    • 1970-01-01
    • 2022-01-04
    • 2021-06-07
    • 2018-12-04
    • 2023-03-25
    • 2021-06-04
    • 2021-05-27
    相关资源
    最近更新 更多