简体   繁体   English

如何使用 Typeorm 向 PostgresQL 数据库添加“关系”?

[英]How do I add a `relation` to my PostgresQL database using Typeorm?

For example例如

@Entity()
class Post {

  @Column()
  post_hash: string;

  @Column()
  title: string;

  categorys:  Array<Category> = []; 
}

@Entity()
class Category {

  @Column()
  content: string;

  @Column()
  post_hash: number;

}

I want to query all the category content of the corresponding post through Typeorm.我想通过Typeorm查询对应post所有category内容。

I tried this method and failed.我试过这个方法,失败了。

this.createQueryBuilder('Post').leftJoinAndMapOne(
            'Post.categorys',
             Category,
            'category',
            'category.post_hash = post.post_hash'
      )

This is my error message.这是我的错误信息。 QueryFailedError: relation "post" does not exist

Entities实体

First of all, you must identify each row with a unique ID using @PrimaryColumn() or@PrimaryGeneratedColumn() decorators.首先,您必须使用@PrimaryColumn()@PrimaryGeneratedColumn()装饰器来标识具有唯一ID 的每一行

Each Post entity can have multiple categories and each Category entity is related to multiple posts .每个Post实体可以有多个类别,并且每个Category实体与多个职位 This is called a many-to-many relation and must be correctly mapped using the @ManyToMany() decorator.这称为多对多关系,必须使用@ManyToMany()装饰器正确映射。 See this for more information.有关更多信息,请参阅内容。

@Entity()
class Post {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  post_hash: string;

  @Column()
  title: string;

  @ManyToMany(() => Category, category => category.posts)
  @JoinTable()
  categories: Category[];
}
@Entity()
class Category {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  content: string;

  @Column()
  post_hash: number;

  @ManyToMany(() => Post, post => post.categories)
  posts: Post[];
}

Query询问

Obtain all Post (s) with the related categories .获取所有具有相关类别的Post

Using find :使用find

const posts: Post[] = await getRepository(Post)
                             .find({ relations: ['categories'] });

Using QueryBuilder :使用QueryBuilder

const posts: Post[] = await getRepository(Post)
                             .createQueryBuilder('post')
                             .leftJoinAndSelect('post.categories', 'category')
                             .getMany();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据 output 添加到我的表 postgresql 中? - How do I add data output to my table in postgresql? 将 Typeorm 添加到我的项目后,我无法进行 postgresql 查询 - After adding Typeorm to My project I cannot make postgresql queries 如何自动将当前日期时间添加到数据库中 - How do I automatically add the current datetime into my database 如何将(sql)数据库添加到我的sintra应用程序? - How do I add (sql) database to my sintra application? 如何确保具有相同外键的所有行在我的 PostgreSQL 数据库中具有唯一名称? - How do I make sure that all rows with the same foreign key have unique names in my PostgreSQL database? 如何更改 Typeorm 中列的别名? - How do I change the alias of a column in Typeorm? 如何将此 SQL 查询转换为 Typeorm - How do I translate this SQL query to Typeorm 如何使用 PHP 中的表单将数据更新到我的 PostgreSQL 数据库? - How can I update data to my PostgreSQL database using a form in PHP? 如何用数据库中的信息填充HashMap,然后将其添加到Jasper报告中? - How do I fill my HashMap with information from my database, and then add it to my Jasper Report? 如何在 SQLite 中显示我的表/关系的属性? - How do i show the attributes of my table/relation in SQLite?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM