代码之家  ›  专栏  ›  技术社区  ›  rt_

TypeOrm-自定义多对多不拉关系数据

  •  0
  • rt_  · 技术社区  · 6 年前

    因为我不想使用cascade来更新联接表,而且我想要自定义列,所以我创建了一个自定义的多对多关系。然而,当我查询关系时,它只提供联接表中的值,而不提取关系数据。

    使用者

    @Entity('user')
    export class User {
      @PrimaryColumn()
      id: string;
    
      @OneToMany(
        () => UserArtistFollowing,
        (userArtistFollowing) => userArtistFollowing.user
      )
      following: UserArtistFollowing[];
    }
    

    艺术家

    
    @Entity('artist')
    export class Artist {
      @PrimaryGeneratedColumn('uuid')
      id: string;
    
      @OneToMany(
        () => UserArtistFollowing,
        (userArtistFollowing) => userArtistFollowing.artist
      )
      usersFollowing: UserArtistFollowing[];
    
    }
    

    用户艺术家跟踪

    
    @Entity('userArtistFollowing')
    export class UserArtistFollowing {
      @PrimaryGeneratedColumn('uuid')
      id: string;
    
      @Column()
      userId!: string;
    
      @Column()
      artistId!: string;
    
      @ManyToOne(
        () => User,
        (user) => user.following
      )
      user!: User;
    
      @ManyToOne(
        () => Artist,
        (artist) => artist.usersFollowing
      )
      artist!: Artist;
    
      @CreateDateColumn()
      createdAt!: Date;
    
      @UpdateDateColumn()
      updatedAt!: Date;
    }
    

    查询

        const user = await getManager()
          .getRepository(Models.User)
          .findOne({
            where: { id: id },
            relations: ['following'],
          });
    

    因此,该查询只提供id、userId、artistId,而不提供对象的艺术家数组(这是我需要的数据)。

    思想?

    0 回复  |  直到 6 年前
        1
  •  6
  •   rt_    6 年前

    经过进一步测试,您可以使用find->与这些习俗的关系多对多的关系。对于嵌套关系,只需用点符号指定关系即可。

          const user = await getManager()
            .getRepository(Models.User)
            .findOne({
              where: { id },
              relations: [
    // you have to specify the join table first (following) to retrieve the columns in the join table
                'following',
    // then you use dotted notation to get the relation from the join table
                'following.artist',
    // another example of a deeply nested relation
                'favourites',
                'favourites.song',
                'favourites.song.supportingArtists',
                'favourites.song.supportingArtists.artist',
              ],
            });
    
    

    也可以将join与嵌套的leftJoinAndSelect一起使用,但这更繁琐。

        const user = await getManager()
          .getRepository(Models.User)
          .findOne({
            where: { id },
            join: {
              alias: 'user',
              leftJoinAndSelect: {
                following: 'user.following',
                artists: 'following.artist',
              },
            },
          });
    

    以下是更新的实体

    用户艺术家跟踪

    @Entity('userArtistFollowing')
    export class UserArtistFollowing {
      @PrimaryColumn()
      userId: string;
    
      @PrimaryColumn()
      artistId: string;
    
      @ManyToOne(
        () => User,
        (user) => user.following
      )
      user!: User;
    
      @ManyToOne(
        () => Artist,
        (artist) => artist.usersFollowing
      )
      artist!: Artist;
    
      @CreateDateColumn()
      createdAt!: Date;
    
      @UpdateDateColumn()
      updatedAt!: Date;
    }
    
    

    艺术家

    @Entity('artist')
    export class Artist {
      @PrimaryGeneratedColumn('uuid')
      id: string;
    
      @OneToMany(
        () => UserArtistFollowing,
        (userArtistFollowing) => userArtistFollowing.artist
      )
      usersFollowing: UserArtistFollowing[];
    }
    
    

    使用者

    @Entity('user')
    export class User {
      @PrimaryColumn()
      id: string;
    
      @OneToMany(
        () => UserArtistFollowing,
        (userArtistFollowing) => userArtistFollowing.user
      )
      following: UserArtistFollowing[];
    }
    
    
        2
  •  0
  •   elthrasher    6 年前

    如果你总是想让艺术家的模特全身水分充足,你可以尝试设置 eager: true 在你的关系选项中。

    https://typeorm.io/#/eager-and-lazy-relations/eager-relations

    如果你不想这样,那么你的解决方案是有意义的。