首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >go-gorm,postgres:简单插入返回错误

go-gorm,postgres:简单插入返回错误
EN

Stack Overflow用户
提问于 2017-08-01 23:22:45
回答 2查看 407关注 0票数 0

由于最近的炒作,我正在尝试做简单的插入,试图评估Postgres的有用性。我是mongoDB的一员。这就是我想要做的:

代码语言:javascript
复制
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()

模型是

代码语言:javascript
复制
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
}

输出:

代码语言:javascript
复制
go run main.go migrate
migrate called
2017/08/01 17:10:26 create name<nil>
exit status 1

我做错了什么?为什么没有错误消息?我该如何修复它?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-08-01 23:36:25

根据docsCreate返回一个*DB,而不是一个错误。所以你的代码应该更像这样

代码语言:javascript
复制
if tx = tx.Create(name); tx.Error != nil { /* ... */ }
票数 1
EN

Stack Overflow用户

发布于 2017-08-01 23:45:25

另外,另外,

代码语言:javascript
复制
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()

我需要更改结构,以便它们有一个相关的字段。我确实对orm有更高的期望。这就是为什么它是一个ORM对象关系管理器。gorm在那里管理的并不多;)

代码语言:javascript
复制
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
}

请注意CustomerID uint

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45441783

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档