首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >3多对多typeOrm

3多对多typeOrm
EN

Stack Overflow用户
提问于 2020-04-29 11:38:01
回答 1查看 588关注 0票数 0

在typeOrm中有实现3种多对多关系的方法吗?

一个3种方式的多到多关系的例子就像

3.多对多关系

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-29 19:44:38

我找到了一个解决方案;它实际上是写在typorm的文档https://github.com/typeorm/typeorm/blob/master/docs/many-to-many-relations.md#many-to-many-relations-with-custom-properties中的

多到多的关系与自定义属性,如果你需要有额外的属性,你的多到多的关系,你必须自己创建一个新的实体。例如,如果希望实体Post和类别与附加的order列具有多到多的关系,则需要创建实体PostToCategory,其中两个ManyToOne关系指向方向和自定义列:

代码语言:javascript
复制
import { Entity, Column, ManyToOne, PrimaryGeneratedColumn } from "typeorm";
import { Post } from "./post";
import { Category } from "./category";

@Entity()
export class PostToCategory {
@PrimaryGeneratedColumn()
public postToCategoryId!: number;

@Column()
public postId!: number;

@Column()
public categoryId!: number;

@Column()
public order!: number;

@ManyToOne(type => Post, post => post.postToCategories)
public post!: Post;

@ManyToOne(type => Category, category => category.postToCategories)
public category!: Category;
}

此外,您还必须向Post和类别添加如下关系:

代码语言:javascript
复制
// category.ts
...
@OneToMany(type => PostToCategory, postToCategory => postToCategory.category)
public postToCategories!: PostToCategory[];

// post.ts
...
@OneToMany(type => PostToCategory, postToCategory => postToCategory.post)
public postToCategories!: PostToCategory[];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61500908

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档