Gorm auto-migration 创建一个没有 user-defined 属性的 table (postgresql)

Gorm auto-migration creating a table with no user-defined attributes (postgresql)

package main

import (
    "fmt"

    _ "github.com/jinzhu/gorm/dialects/postgres"
    "gorm.io/driver/postgres"
    "gorm.io/gorm"
)

type Books struct {
    gorm.Model
    ID              uint
    title           string
    author          string
    description     string
    display_picture byte
}


func main() {
    
    dsn := //successful create connection string
    
    db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})

    if err != nil {
        // fmt.Println(err)
        panic(err)
    }
    b := Books{}
    db.AutoMigrate(&b)
    data := Books{
        title:       "Invisible Cities",
        author:      "Italio Calvino",
        description: "This book shows you the power of prose. It's a beautiful read which will make you feel like you're floating on a cloud.",
    }
    db.Create(&data)
    fmt.Println(data)
}

连接字符串是正确的,因为 db.AutoMitrate(&b) 连接到数据库并实现了 ID 和 gorm.Model 属性(createdAt 等),但是它没有添加我的属性标题,作者和描述。

我在谷歌上搜索了一整天,但在其他任何地方都找不到这个错误。谁能帮忙?此外,我正在 运行 脚本之前删除我在 postgres 中的书籍 table。

  • 将你的代码重写成这个并永远记住在 Gorm 中我们需要将模型字段大写
package main

import (
    "fmt"

    _ "github.com/jinzhu/gorm/dialects/postgres"
    "gorm.io/driver/postgres"
    "gorm.io/gorm"
)

type Books struct {
    gorm.Model
    ID              uint
    Title           string
    Author          string
    Description     string
    Display_Picture byte
}


func main() {
    
    dsn := //successful create connection string
    
    db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})

    if err != nil {
        // fmt.Println(err)
        panic(err)
    }
    b := Books{}
    db.AutoMigrate(&b)
    data := Books{
        title:       "Invisible Cities",
        author:      "Italio Calvino",
        description: "This book shows you the power of prose. It's a beautiful read which will make you feel like you're floating on a cloud.",
    }
    db.Create(&data)
    fmt.Println(data)
}