【问题标题】:mysql Column 'id' in where clause is ambiguouswhere子句中的mysql列'id'不明确
【发布时间】:2021-05-30 04:39:33
【问题描述】:

我是 mysql 和 typeorm 的新手,我正在尝试获取属于特定用户的所有帖子,我想使用 id 进行此操作,但我得到了Column 'id' in where clause is ambiguous

这是我的服务

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { createQueryBuilder } from 'typeorm';
import { Repository } from 'typeorm/repository/Repository';
import { User } from './user.entity';

@Injectable()
export class UserService {
  constructor(
    @InjectRepository(User)
    private readonly userRepository: Repository<User>,
  ) {}
 
  async userPosts(id): Promise<User> {
    return this.userRepository
      .createQueryBuilder('user')
      .leftJoinAndSelect('user.posts', 'post')
      .where('user.id = :id', { id: id }) // whats the problem here ?
      .getOne();
  }
 
}

这就是我的用户实体

import { Post } from 'src/post/post.entity';
import { Entity, Column, PrimaryGeneratedColumn, OneToMany } from 'typeorm';

@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  firstName: string;

  @Column()
  lastName: string;

  @Column({ default: true })
  isActive: boolean;

  @OneToMany(() => Post, (post: Post) => post.user, {
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE',
  })
  posts: Post[];
}

【问题讨论】:

  • 我请求你分享一个表userposts的CREATE语句

标签: javascript mysql typescript nestjs typeorm


【解决方案1】:

{ id: id } 您没有指定 id 字段来自哪个表。你的意思是: { user.id: id }

【讨论】:

  • 这应该不是问题
猜你喜欢
  • 2014-05-03
  • 2010-09-25
  • 1970-01-01
  • 1970-01-01
  • 2019-04-05
  • 1970-01-01
  • 2017-07-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多