Skip to content
This repository was archived by the owner on Mar 18, 2022. It is now read-only.

Commit c8a9ea0

Browse files
maxsommerpleerock
authored andcommitted
docs: add explanation ManyToMany with custom properties (typeorm#4308)
* Add explanation ManyToMany with custom properties Since I myself ran into the issue and had to rewrite my code based on this solution I hope it may be helpful for others as well 😊 * Update explanation with more relateable example Update explanation with abstract example to be more relateable Add cross link to faq * Update phrasing, unnecessary explanation and code Remove unnecessary explanation upfront mentioning FAQ Update phrasing of main explanatory paragraph to be more precise Update code example to work correctly
1 parent 79bf9f7 commit c8a9ea0

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

docs/faq.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ join column / junction table settings, like join column name or junction table n
131131
It's not possible to add extra columns into a table created by a many-to-many relation.
132132
You'll need to create a separate entity and bind it using two many-to-one relations with the target entities
133133
(the effect will be same as creating a many-to-many table),
134-
and add extra columns in there.
134+
and add extra columns in there. You can read more about this in [Many-to-Many relations](./many-to-many-relations.md#many-to-many-relations-with-custom-properties).
135135

136136
## How to use TypeORM with a dependency injection tool?
137137

docs/many-to-many-relations.md

+37
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,40 @@ const categoriesWithQuestions = await connection
169169
.leftJoinAndSelect("category.questions", "question")
170170
.getMany();
171171
```
172+
173+
## many-to-many relations with custom properties
174+
175+
In case you need to have additional properties to your many-to-many relationship you have to create a new entity yourself.
176+
For example if you would like entities `Post` and `Category` to have a many-to-many relationship with a `createdAt` property
177+
associated to it you have to create entity `PostToCategory` like the following:
178+
179+
```typescript
180+
import { Entity, Column, ManyToOne, PrimaryGeneratedColumn } from "typeorm";
181+
import { Post } from "./post";
182+
import { Category } from "./category";
183+
184+
@Entity()
185+
export class PostToCategory {
186+
@PrimaryGeneratedColumn()
187+
public postToCategoryId!: number;
188+
189+
public postId!: number;
190+
public categoryId!: number;
191+
192+
@Column()
193+
public order!: number;
194+
195+
@ManyToOne(type => Post, post => post.postToCategories)
196+
public post!: Post;
197+
198+
@ManyToOne(type => Category, category => category.postToCategories)
199+
public category!: Category;
200+
}
201+
```
202+
203+
Additionally you will have to add a relationship like the following to `Post` and `Category`:
204+
205+
```typescript
206+
@OneToMany((type) => PostToCategory, (postToCategory) => postToCategory.post)
207+
public postToCategories!: PostToCategory[];
208+
```

0 commit comments

Comments
 (0)