英文:
go-gorm, postgres: simple inserts return error
问题
我正在尝试进行简单的插入操作,想要评估Postgres的实用性,因为最近有点炒作。我是一个MongoDB的用户。以下是我正在尝试做的事情:
db, e := gorm.Open("postgres", fmt.Sprintf("host=%s user=%s dbname=%s password=%s sslmode=disable", pgHost, pgUser, pgDatabase, pgPass))
if e != nil {
log.Fatal(e.Error())
}
defer db.Close()
db.AutoMigrate(&model.Customer{}, &model.Email{}, &model.Address{}, &model.Name{}, &model.Logindata{})
name := new(model.Name)
email := new(model.Email)
customer := &model.Customer{
Name: name,
PrimaryEmail: email,
}
customer.Name.First = "Darko"
customer.Name.Last = "Luketic"
customer.Name.Middle = "" // also tried without this line
customer.PrimaryEmail.Address = "my@mail.come"
customer.PrimaryEmail.Verified = true
tx := db.Begin()
if e := tx.Create(name); e != nil {
tx.Rollback()
log.Fatal("create name", e.Error)
}
if e := tx.Create(email); e != nil {
tx.Rollback()
log.Fatal("create email", e.Error)
}
if e := tx.Create(customer); e != nil {
tx.Rollback()
log.Fatal("create customer", e.Error)
}
tx.Commit()
其中模型定义如下:
package model
import "github.com/jinzhu/gorm"
type Customer struct {
gorm.Model
Name *Name
BillingAddress *Address
ShippingAddress *Address
PrimaryEmail *Email
AlternateEmails []*Email
Logindata *Logindata
}
type Name struct {
gorm.Model
First string `json:"first"`
Middle string `json:"middle"`
Last string `json:"last"`
}
type Logindata struct {
gorm.Model
Username string
Password []byte
}
type Email struct {
gorm.Model
Address string
Verified bool
}
type Address struct {
gorm.Model
Address1 string
Address2 string
City string
Code string
Country string
}
输出结果为:
go run main.go migrate
migrate called
2017/08/01 17:10:26 create name<nil>
exit status 1
我做错了什么?
为什么没有错误消息?
我该如何修复它?
英文:
I'm trying to do simple inserts, trying to evaluate the usefulness of Postgres because of this recent hype. I'm a mongoDB guy. And this is what I'm trying to do:
db, e := gorm.Open("postgres", fmt.Sprintf("host=%s user=%s dbname=%s password=%s sslmode=disable", pgHost, pgUser, pgDatabase, pgPass))
if e != nil {
log.Fatal(e.Error())
}
defer db.Close()
db.AutoMigrate(&model.Customer{}, &model.Email{}, &model.Address{}, &model.Name{}, &model.Logindata{})
name := new(model.Name)
email := new(model.Email)
customer := &model.Customer{
Name: name,
PrimaryEmail: email,
}
customer.Name.First = "Darko"
customer.Name.Last = "Luketic"
customer.Name.Middle = "" // also tried without this line
customer.PrimaryEmail.Address = "my@mail.come"
customer.PrimaryEmail.Verified = true
tx := db.Begin()
if e := tx.Create(name); e != nil {
tx.Rollback()
log.Fatal("create name", e.Error)
}
if e := tx.Create(email); e != nil {
tx.Rollback()
log.Fatal("create email", e.Error)
}
if e := tx.Create(customer); e != nil {
tx.Rollback()
log.Fatal("create customer", e.Error)
}
tx.Commit()
with the models being
package model
import "github.com/jinzhu/gorm"
type Customer struct {
gorm.Model
Name *Name
BillingAddress *Address
ShippingAddress *Address
PrimaryEmail *Email
AlternateEmails []*Email
Logindata *Logindata
}
type Name struct {
gorm.Model
First string `json:"first"`
Middle string `json:"middle"`
Last string `json:"last"`
}
type Logindata struct {
gorm.Model
Username string
Password []byte
}
type Email struct {
gorm.Model
Address string
Verified bool
}
type Address struct {
gorm.Model
Address1 string
Address2 string
City string
Code string
Country string
}
The output:
go run main.go migrate
migrate called
2017/08/01 17:10:26 create name<nil>
exit status 1
What am I doing wrong?
Why is there no error message?
How do I fix it?
答案1
得分: 1
根据文档的说明,Create
方法返回的是*DB
类型,而不是错误。所以你的代码应该更像这样:
if tx = tx.Create(name); tx.Error != nil { /* ... */ }
英文:
According to the docs, Create
returns a *DB
, not an error. So your code should be more like
if tx = tx.Create(name); tx.Error != nil { /* ... */ }
答案2
得分: 0
此外,
db,e:= gorm.Open(“ postgres”,fmt.Sprintf(“ host =%s user =%s dbname =%s password =%s sslmode = disable”,pgHost,pgUser,pgDatabase,pgPass))
如果e!= nil {
log.Fatal(e.Error())
}
defer db.Close()
db.AutoMigrate(&model.Customer {},&model.Email {},&model.Address {},&model.Name {},&model.Logindata {})
name:= new(model.Name)
email:= new(model.Email)
customer:=&model.Customer {
Name:name,
PrimaryEmail:email,
}
customer.Name.First =“ Darko”
customer.Name.Last =“ Luketic”
customer.Name.Middle =“”
customer.PrimaryEmail.Address =“ my@mail.come”
customer.PrimaryEmail.Verified = true
tx:= db.Begin()
如果e:= tx.Create(customer).Error; e!= nil {
tx.Rollback()
log.Fatal(“创建客户”,e)
}
tx.Commit()
我需要更改结构,使它们具有相关字段。我对orm期望更多。这就是为什么它是一个ORM对象关系管理器。
gorm在那里没有管理太多;)
包模型
import "github.com/jinzhu/gorm"
类型客户 struct {
gorm.Model
Name *Name
BillingAddress *Address
ShippingAddress *Address
PrimaryEmail *Email
AlternateEmails []*Email
Logindata *Logindata
}
类型名称 struct {
gorm.Model
CustomerID uint
First string json:"first"
Middle string json:"middle"
Last string json:"last"
}
类型Logindata struct {
gorm.Model
CustomerID uint
Username string
Password []byte
}
类型电子邮件 struct {
gorm.Model
CustomerID uint
Address string
Verified bool
}
类型地址 struct {
gorm.Model
CustomerID uint
Address1 string
Address2 string
City string
Code string
Country string
}
注意CustomerID uint
英文:
Also, additionally,
db, e := gorm.Open("postgres", fmt.Sprintf("host=%s user=%s dbname=%s password=%s sslmode=disable", pgHost, pgUser, pgDatabase, pgPass))
if e != nil {
log.Fatal(e.Error())
}
defer db.Close()
db.AutoMigrate(&model.Customer{}, &model.Email{}, &model.Address{}, &model.Name{}, &model.Logindata{})
name := new(model.Name)
email := new(model.Email)
customer := &model.Customer{
Name: name,
PrimaryEmail: email,
}
customer.Name.First = "Darko"
customer.Name.Last = "Luketic"
customer.Name.Middle = ""
customer.PrimaryEmail.Address = "my@mail.come"
customer.PrimaryEmail.Verified = true
tx := db.Begin()
if e := tx.Create(customer).Error; e != nil {
tx.Rollback()
log.Fatal("create customer", e)
}
tx.Commit()
I need to change the structs so they have a related field. I did expect more from an orm. That's why it's an ORM object relationship manager.
gorm doesn't manage much there
package model
import "github.com/jinzhu/gorm"
type Customer struct {
gorm.Model
Name *Name
BillingAddress *Address
ShippingAddress *Address
PrimaryEmail *Email
AlternateEmails []*Email
Logindata *Logindata
}
type Name struct {
gorm.Model
CustomerID uint
First string `json:"first"`
Middle string `json:"middle"`
Last string `json:"last"`
}
type Logindata struct {
gorm.Model
CustomerID uint
Username string
Password []byte
}
type Email struct {
gorm.Model
CustomerID uint
Address string
Verified bool
}
type Address struct {
gorm.Model
CustomerID uint
Address1 string
Address2 string
City string
Code string
Country string
}
note the CustomerID uint
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论