【问题标题】:TypeORM: Joining when we have one to many and many to one relationshipTypeORM:当我们有一对多和多对一关系时加入
【发布时间】:2019-01-03 09:18:06
【问题描述】:
@Entity()
export class User {
  @PrimaryColumn()
  id: string;

  @Column({unique: true})
  username: string;

  @Column({unique: true})
  email: string;

  @OneToMany(type => Post, post => post.id)
  posts: Post[];
}

@Entity()
export class Post {

  @PrimaryGeneratedColumn()
  id: number;

  @ManyToOne(type => User, user => user.posts)
  @JoinColumn({name: 'user_id'})
  user: User;

  @OneToMany(type => Image, image => image.id)
  images: Image[];
}
 
@Entity()
export class Image {
  @PrimaryGeneratedColumn()
  id: number;

  @ManyToOne(type => Post, post => post.images)
  @JoinColumn({name : 'post_id'})
  post: Post;
}

我有这 3 个实体,我想进行查询以获取用户的所有帖子,并为该帖子获取所有图像。我正在尝试使用以下代码执行此操作:

return await this.postRepository.createQueryBuilder("post")
  .innerJoinAndSelect("post.images", "image")
  .where("user_id = :userId", {userId: id})
  .getMany();

我收到以下错误:

Cannot read property 'joinColumns' of undefined

我也试过这个而不是上面的.innerJoin

.innerJoinAndSelect(Image, "image", "image.post_id = post.id")

这样我就不会再收到那个错误了,但结果我只得到了帖子,我没有从中得到图片

【问题讨论】:

  • 进展如何?问题解决了吗?

标签: nestjs typeorm


【解决方案1】:
@Entity()
export class User {

 @OneToMany(type => Post, post => post.user)
 posts: Post[];
}

@Entity()
export class Post {

 @PrimaryGeneratedColumn()
 id: number;

 @Column()
 user_id: number;

 @ManyToOne(type => User)
 @JoinColumn({name: 'user_id', referencedColumnName: 'id'})
 user: User;

 @OneToMany(type => Image, image => image.post)
 images: Image[];
}

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

 @Column()
 post_id: number;

 @ManyToOne(type => Post)
 @JoinColumn({name : 'post_id', referencedColumnName: 'id'})
 post: Post;
}

试试这个

【讨论】:

  • 这行得通。将添加具有外键的列。非常感谢
  • 谢谢!我错过了@JoinColumn 装饰器
  • 使用这个我得到:“'type' 已声明,但它的值从未被读取。”
  • referencedColumnName: 'id' 对我有用!
【解决方案2】:

我自己也遇到过同样的问题。

你应该在这里改变关系:

@OneToMany(type => Image, image => image.id)
images: Image[];

到这里:

@OneToMany(type => Image, image => image.post)
images: Image[];

请注意,image.id 应该是 image.post 而不是匹配反面。

*编辑 => 这里出现了同样的问题:

@OneToMany(type => Post, post => post.id)
posts: Post[];

这个关系也应该有反面,post.id应该是post.user

@OneToMany(type => Post, post => post.user)
posts: Post[];

注意这一点,因为它直到运行时才会抛出任何错误。

我刚刚用上面的修改测试了这个查询,错误消失了:

return await this.postRepository.find({
  relations: ['images', 'user'],
  where: { user: { id: id } },
});

您也可以省略 @JoinColumn() 装饰器,因为它们在一对多多对一关系中不是必需的,你可以看到官方文档中提到的:

您可以在 @ManyToOne / @OneToMany 关系中省略 @JoinColumn。没有@ManyToOne@OneToMany 就无法存在。如果要使用@OneToMany,则需要@ManyToOne。您在哪里设置 @ManyToOne - 它的相关实体将具有“关系 id”和外键。

请参阅TypeORM documentation 了解有关此的更多详细信息,您还将找到此类关系的示例。

【讨论】:

  • 使用这个方法我得到一个错误:'type'被声明但它的值从未被读取。
  • 您应该能够通过将 'type' 替换为 () 或 _(空括号或下划线)来解决这个问题,让 typescript 编译器知道您不想使用它。您可以在此答案中找到更多信息stackoverflow.com/a/41086381/10952954
  • TypeORM 文档的问题在于它假定您正在使用同步功能,因此库创建 连接列以便它知道它是哪一个。但它并没有清楚地解释如何处理未同步的已经存在的表。
猜你喜欢
  • 2020-09-01
  • 2020-06-25
  • 2020-06-29
  • 2021-12-23
  • 1970-01-01
  • 2011-08-29
  • 2021-05-18
  • 2015-02-05
  • 1970-01-01
相关资源
最近更新 更多