Gorm自动迁移在PostgreSQL中创建一个没有用户定义属性的表。

huangapple go评论102阅读模式
英文:

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 := //成功创建连接字符串

	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.AutoMigrate(&b)可以连接到数据库并实现ID和gorm.Model属性(如createdAt等),但它没有添加我的属性title、author和description。

我已经整天在谷歌上搜索,但找不到这个错误的解决方法。有人可以帮忙吗?另外,在运行脚本之前,我会删除我的Postgres中的Books表。

英文:
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)
}

The connection string is correct because the db.AutoMitrate(&b) connects with the database and implements the ID and the gorm.Model attributes (createdAt etc) however it doesn't add my attributes title, author and description.

I've spend the whole day googling but I cant find this error anywhere else. Can anyone help? Also I am deleting my Books table in postgres before running the script.

答案1

得分: 1

请注意,我将为您提供代码的翻译版本,但是由于代码中存在一些特殊字符和格式,可能会导致翻译结果不完全准确。以下是您提供的代码的翻译版本:

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 := //成功创建连接字符串
    
    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: "这本书展示了散文的力量。它是一本美丽的阅读,会让你感觉像在云上漂浮。",
    }
    db.Create(&data)
    fmt.Println(data)
}

希望这可以帮助到您!如果您有任何其他问题,请随时提问。

英文:
  • Rewrite your code to this and always remember in Gorm we need to have Model Fields Capitalised
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)
}

huangapple
  • 本文由 发表于 2021年6月6日 09:45:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/67855194.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定