docs: update the docs to keep in line with the example (#10387)

This commit is contained in:
Christopher Kiessling 2023-12-29 08:13:43 -03:00 committed by GitHub
parent 7de4890265
commit 6d5b5d9ba6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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[];
```