typeorm为什么createquerybuilder LeftJoin和Select with condition和with with condition但with where具有不同的结果?

htrmnn0y  于 2021-09-23  发布在  Java
关注(0)|答案(0)|浏览(225)

回购与示例项目:https://github.com/fblfbl/backend-movies
基于nest.js和typeorm(postgresql)的项目
共有3个实体:
电影
使用者
userdetails(用户最喜欢/观看的电影)
只有在用户将电影添加到“收藏/观看”中后,才能创建电影的userdetails(和用户的userdetails)。我希望通过左键连接为用户获取所有包含userdetails和不包含userdetails的电影。
仅当我使用此存储库方法时,它才起作用:

async findAllByUser(id: number) {
    return this.createQueryBuilder('movie')
      .leftJoinAndSelect(
        'movie.userDetails',
        'userDetails',
        `movie.id = userDetails.movieId AND userDetails.userId = ${id}`,
      )
      .getMany();
  }

下面是示例结果:

[
    {
        "id": 1,
        "title": "test1",
        "genre": "test1",
        "userDetails": {
            "id": 1,
            "isFavorite": false,
            "isWatched": true,
            "isInWatchlist": false,
            "watchingDate": "2021-07-25T19:29:18.510Z",
            "userId": 1,
            "movieId": 1
        }
    },
    {
        "id": 2,
        "title": "test2",
        "genre": "test2",
        "userDetails": {
            "id": 2,
            "isFavorite": true,
            "isWatched": true,
            "isInWatchlist": false,
            "watchingDate": "2021-07-26T13:08:21.533Z",
            "userId": 1,
            "movieId": 2
        }
    },
    {
        "id": 3,
        "title": "test3",
        "genre": "test3",
        "userDetails": {
            "id": 3,
            "isFavorite": true,
            "isWatched": true,
            "isInWatchlist": true,
            "watchingDate": "2021-07-26T13:09:11.852Z",
            "userId": 1,
            "movieId": 3
        }
    },
    {
        "id": 4,
        "title": "test4",
        "genre": "test4",
        "userDetails": null
    }
]

但是如果我使用下两种方法中的一种而不是前一种:

async findAllByUser(id: number) {
    return this.createQueryBuilder('movie')
      .leftJoinAndSelect('movie.userDetails', 'userDetails')
      .where(`movie.id = userDetails.movieId`)
      .andWhere('userDetails.userId = :userId', { userId: id })
      .getMany();
  }
async findAllByUser(id: number) {
    return this.find({
      relations: ['userDetails'],
      join: {
        alias: 'movie',
        leftJoinAndSelect: { userDetails: 'movie.userDetails' },
      },
      where: (qb) => {
        qb.where('userDetails.userId = :userId', { userId: id });
      },
    });
  }

我得到这个(没有id为4的电影没有用户详细信息):

[
    {
        "id": 1,
        "title": "test1",
        "genre": "test1",
        "userDetails": {
            "id": 1,
            "isFavorite": false,
            "isWatched": true,
            "isInWatchlist": false,
            "watchingDate": "2021-07-25T19:29:18.510Z",
            "userId": 1,
            "movieId": 1
        }
    },
    {
        "id": 2,
        "title": "test2",
        "genre": "test2",
        "userDetails": {
            "id": 2,
            "isFavorite": true,
            "isWatched": true,
            "isInWatchlist": false,
            "watchingDate": "2021-07-26T13:08:21.533Z",
            "userId": 1,
            "movieId": 2
        }
    },
    {
        "id": 3,
        "title": "test3",
        "genre": "test3",
        "userDetails": {
            "id": 3,
            "isFavorite": true,
            "isWatched": true,
            "isInWatchlist": true,
            "watchingDate": "2021-07-26T13:09:11.852Z",
            "userId": 1,
            "movieId": 3
        }
    }
]

所有方法都使用左连接,但为什么结果不同呢?你能解释一下吗

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题