mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
docs: update the docs to keep in line with the example (#10387)
This commit is contained in:
parent
7de4890265
commit
6d5b5d9ba6
@ -259,20 +259,20 @@ const categoriesWithQuestions = await dataSource
|
||||
## Many-to-many relations with custom properties
|
||||
|
||||
In case you need to have additional properties in your many-to-many relationship, you have to create a new entity yourself.
|
||||
For example, if you would like entities `Post` and `Category` to have a many-to-many relationship with an additional `order` column, then you need to create an entity `PostToCategory` with two `ManyToOne` relations pointing in both directions and with custom columns in it:
|
||||
For example, if you would like entities `Question` and `Category` to have a many-to-many relationship with an additional `order` column, then you need to create an entity `QuestionToCategory` with two `ManyToOne` relations pointing in both directions and with custom columns in it:
|
||||
|
||||
```typescript
|
||||
import { Entity, Column, ManyToOne, PrimaryGeneratedColumn } from "typeorm"
|
||||
import { Post } from "./post"
|
||||
import { Question } from "./question"
|
||||
import { Category } from "./category"
|
||||
|
||||
@Entity()
|
||||
export class PostToCategory {
|
||||
export class QuestionToCategory {
|
||||
@PrimaryGeneratedColumn()
|
||||
public postToCategoryId: number
|
||||
public questionToCategoryId: number
|
||||
|
||||
@Column()
|
||||
public postId: number
|
||||
public questionId: number
|
||||
|
||||
@Column()
|
||||
public categoryId: number
|
||||
@ -280,24 +280,24 @@ export class PostToCategory {
|
||||
@Column()
|
||||
public order: number
|
||||
|
||||
@ManyToOne(() => Post, (post) => post.postToCategories)
|
||||
public post: Post
|
||||
@ManyToOne(() => Question, (question) => question.questionToCategories)
|
||||
public question: Question
|
||||
|
||||
@ManyToOne(() => Category, (category) => category.postToCategories)
|
||||
@ManyToOne(() => Category, (category) => category.questionToCategories)
|
||||
public category: Category
|
||||
}
|
||||
```
|
||||
|
||||
Additionally you will have to add a relationship like the following to `Post` and `Category`:
|
||||
Additionally you will have to add a relationship like the following to `Question` and `Category`:
|
||||
|
||||
```typescript
|
||||
// category.ts
|
||||
...
|
||||
@OneToMany(() => PostToCategory, postToCategory => postToCategory.category)
|
||||
public postToCategories: PostToCategory[];
|
||||
@OneToMany(() => questionToCategory, questionToCategory => questionToCategory.category)
|
||||
public questionToCategories: QuestionToCategory[];
|
||||
|
||||
// post.ts
|
||||
// question.ts
|
||||
...
|
||||
@OneToMany(() => PostToCategory, postToCategory => postToCategory.post)
|
||||
public postToCategories: PostToCategory[];
|
||||
@OneToMany(() => QuestionToCategory, questionToCategory => questionToCategory.question)
|
||||
public questionToCategories: QuestionToCategory[];
|
||||
```
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user