Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
198 views
in Technique[技术] by (71.8m points)

sql - How to load multiple one-to-many relations in Typeorm / PostgreSQL?

I have 3 tables A, B, C.

  • A is the main table
  • B & C have a many-to-one relation with A

Let's say the data is as follow:

Table A                  Table B                            Table C
|  id   | name  |        |  id   | a_id (FK) | value |      |  id   | a_id (FK) | value |
| :---: | :---: |        | :---: | :-------: | :---: |      | :---: | :-------: | :---: |
|  a0   |  A0   |        |  b0   |    a0     |  B0   |      |  c0   |    a0     |  C0   |
|  a1   |  A1   |        |  b1   |    a0     |  B1   |      |  c1   |    a0     |  C1   |
                         |  b2   |    a1     |  B2   |

I want to retrieve the records in A with their relations aggregated into an array or a json, like so:

id name b c
a0 A0 [[b0, B0], [b1, B1]] [[c0, C0], [c1, C1]]
a1 A1 [[b2, B2]] []
question from:https://stackoverflow.com/questions/65649628/how-to-load-multiple-one-to-many-relations-in-typeorm-postgresql

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...