我有型号:

type Book struct {
    gorm.Model
    Title       string `json:"title"`
    Author      string `json:"author"`
    Description string `json:"description"`
    Category    string `json:"Category"`
    Publisher   string `json:"publisher"`
    AuthorsCard []*AuthorsCard `gorm:"many2many:book_authorscard; "json:"authorscard"`
}

type AuthorsCard struct {
    gorm.Model
    Name        string `json:"name"`
    Age         int    `json:"age"`
    YearOfBirth int    `json:"year"`
    Biography   string `json:"biography"`
}

After db.AutoMigrate(&models.Book{}, &models.AuthorsCard{})
I have a creation function:

func TestCreate() {
    var testbook = models.Book{
        Title:  "Test",
        Author: "tst",
        AuthorsCard: []*models.AuthorsCard{
            {
                Age:         23,
                Name:        "test",
                YearOfBirth: 1999,
                Biography:   "23fdgsdddTEST",
            },
        },
        Description: "something",
    }
    db.Preload("AuthorsCard").Find(&models.Book{})

    db.Create(&testbook)

}

尽管AuthorsCard中有一些数据,但我收到的回应是:

{
        "ID": 78,
        "CreatedAt": "2022-06-23T13:10:58.01629+03:00",
        "UpdatedAt": "2022-06-23T13:10:58.01629+03:00",
        "DeletedAt": null,
        "title": "Test",
        "author": "tst",
        "description": "something",
        "Category": "",
        "publisher": "",
        "authorscard": null
    }

如您所见,"authorscard"为空.

如何将"authorscard"保存到数据库?

推荐答案

按正确顺序调用代码时,代码正在工作:

func TestCreate() {
    db := getDB()
    db.AutoMigrate(&Book{}, AuthorsCard{})

    var testbook = Book{
        Title:  "Test",
        Author: "tst",
        AuthorsCard: []*AuthorsCard{
            {
                Age:         23,
                Name:        "test",
                YearOfBirth: 1999,
                Biography:   "23fdgsdddTEST",
            },
        },
        Description: "something",
    }

    // 1. Create your testbook.
    db.Create(&testbook)

    // 2. Store it into a variable:
    var b1 *Book
    db.Preload("AuthorsCard").Find(&b1)
    
    fmt.Println(b1.AuthorsCard[0].Age)
    fmt.Println(b1.AuthorsCard[0].Name)
    fmt.Println(b1.AuthorsCard[0].YearOfBirth)
    fmt.Println(b1.AuthorsCard[0].Biography)

}

打印:

23 test 1999 23fdgsdddTEST

此外,您的JSON导出可能会失败,因为您将指针传递给AuthorCard,并且编组在这些情况下并不总是正常工作.然而,戈姆在这里做得很好.

静态判断也给了我一些提示:

type Book struct {
    gorm.Model
    Title       string         `json:"title"`
    Author      string         `json:"author"`
    Description string         `json:"description"`
    Category    string         `json:"Category"`
    Publisher   string         `json:"publisher"`
    AuthorsCard []*AuthorsCard `gorm:"many2many:book_authorscard" json:"authorscard"` // wrong space
}

Postgresql相关问答推荐

在输入稍有错误的PostgreSQL表中进行快速字符串搜索

计算两个子查询之间的差异

可以向判断枚举值的SQL列添加约束吗?

Rust中使用sqlx的多线程postgres操作的惯用方法是什么?

如何使 Postgres 优化引擎具有确定性

在 jOOQ 中使用 $$ 引用字符串

Docker compose read connection reset by peer error on pipeline

从 PostgreSQL 中的每个组中抽取 N 个样本

如何将 DELETE 的返回值插入到 postgresql 中的 INSERT 中?

PostgreSQL INSERT 插入一个枚举数组

在远程机器上Restore dump

如何引用使用 ON CONFLICT 中的函数的唯一索引?

如何使用 WITH RECURSIVE 子句进行 Select

从长时间的postgres转换日期

try 为 ror 应用程序设置 postgres,出现错误 - fe_sendauth:no password supplied

如何在 postgresql 交叉表中用零替换空值

如何使 array_agg() 像 mySQL 中的 group_concat() 一样工作

如何从我的 postgresql 查询中获取最小值、中值和最大值?

PostgreSQL:如何解决numeric field overflow问题

SQL 函数返回类型:TABLE vs SETOF 记录