如何使用GORM库在Golang应用程序中建立多对多关系?

时间:2019-03-03 14:24:10

标签: postgresql go go-gorm

我在我的 Golang 项目中使用了GORM库,我需要帮助。

在我的 PostgreSQL 数据库中,我有4个表。他们叫:surveysquestionsoptionssurveys_questions_options。那就是他们的样子:

调查表:

| survey_id (uuid4)                    | survey_name (varchar) |
|--------------------------------------|-----------------------|
| 0cf1cf18-d5fd-474e-a8be-754fbdc89720 | April                 |
| b9fg55d9-n5fy-s7fe-s5bh-856fbdc89720 | May                   |

问题表:

| question_id (int) | question_text (text)         |
|-------------------|------------------------------|
| 1                 | What is your favorite color? |

OPTIONS 表:

| option_id (int)   | option_text (text) |
|-------------------|--------------------|
| 1                 | red                |
| 2                 | blue               |
| 3                 | grey               |
| 4                 | green              |
| 5                 | brown              |

SURVEYS_QUESTIONS_OPTIONS 表合并了之前的所有三个表:

| survey_id                            | question_id | option_id |
|--------------------------------------|-------------|-----------|
| 0cf1cf18-d5fd-474e-a8be-754fbdc89720 | 1           | 1         |
| 0cf1cf18-d5fd-474e-a8be-754fbdc89720 | 1           | 2         |
| 0cf1cf18-d5fd-474e-a8be-754fbdc89720 | 1           | 3         |
| b9fg55d9-n5fy-s7fe-s5bh-856fbdc89720 | 1           | 3         |
| b9fg55d9-n5fy-s7fe-s5bh-856fbdc89720 | 1           | 4         |
| b9fg55d9-n5fy-s7fe-s5bh-856fbdc89720 | 1           | 5         |

我为前三个表和每个表的CRUD创建结构。不幸的是,我不知道如何在Golang应用程序中正确初始化多对多关系,您可以在最后一个SURVEYS_QUESTIONS_OPTIONS表中注意到这一点。实际上,我需要采用树JSON结构来响应REST服务,例如:

[
    {
        "survey_id": "0cf1cf18-d5fd-474e-a8be-754fbdc89720",
        "survey_name": "April",
        "questions": [
            {
                "question_id": 1,
                "question_text": "What is your favorite color?",
                "options": [
                    {
                        "option_id": 1,
                        "option_text": "red"
                    },
                    {
                        "option_id": 2,
                        "option_text": "blue"
                    },
                    {
                        "option_id": 3,
                        "option_text": "grey"
                    },
                ]
            }
        ]
    },
    {
        "survey_id": "b9fg55d9-n5fy-s7fe-s5bh-856fbdc89720",
        "survey_name": "May",
        "questions": [
            {
                "question_id": 1,
                "question_text": "What is your favorite color?",
                "options": [
                    {
                        "option_id": 3,
                        "option_text": "grey"
                    },
                    {
                        "option_id": 4,
                        "option_text": "green"
                    },
                    {
                        "option_id": 5,
                        "option_text": "brown"
                    },
                ]
            }
        ]
    }
]

有人可以通过小例子向我展示如何做到吗?

0 个答案:

没有答案