英文:
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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论