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

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

go-gorm, postgres: simple inserts return error

问题

我正在尝试进行简单的插入操作,想要评估Postgres的实用性,因为最近有点炒作。我是一个MongoDB的用户。以下是我正在尝试做的事情:

  1. db, e := gorm.Open("postgres", fmt.Sprintf("host=%s user=%s dbname=%s password=%s sslmode=disable", pgHost, pgUser, pgDatabase, pgPass))
  2. if e != nil {
  3. log.Fatal(e.Error())
  4. }
  5. defer db.Close()
  6. db.AutoMigrate(&model.Customer{}, &model.Email{}, &model.Address{}, &model.Name{}, &model.Logindata{})
  7. name := new(model.Name)
  8. email := new(model.Email)
  9. customer := &model.Customer{
  10. Name: name,
  11. PrimaryEmail: email,
  12. }
  13. customer.Name.First = "Darko"
  14. customer.Name.Last = "Luketic"
  15. customer.Name.Middle = "" // also tried without this line
  16. customer.PrimaryEmail.Address = "my@mail.come"
  17. customer.PrimaryEmail.Verified = true
  18. tx := db.Begin()
  19. if e := tx.Create(name); e != nil {
  20. tx.Rollback()
  21. log.Fatal("create name", e.Error)
  22. }
  23. if e := tx.Create(email); e != nil {
  24. tx.Rollback()
  25. log.Fatal("create email", e.Error)
  26. }
  27. if e := tx.Create(customer); e != nil {
  28. tx.Rollback()
  29. log.Fatal("create customer", e.Error)
  30. }
  31. tx.Commit()

其中模型定义如下:

  1. package model
  2. import "github.com/jinzhu/gorm"
  3. type Customer struct {
  4. gorm.Model
  5. Name *Name
  6. BillingAddress *Address
  7. ShippingAddress *Address
  8. PrimaryEmail *Email
  9. AlternateEmails []*Email
  10. Logindata *Logindata
  11. }
  12. type Name struct {
  13. gorm.Model
  14. First string `json:"first"`
  15. Middle string `json:"middle"`
  16. Last string `json:"last"`
  17. }
  18. type Logindata struct {
  19. gorm.Model
  20. Username string
  21. Password []byte
  22. }
  23. type Email struct {
  24. gorm.Model
  25. Address string
  26. Verified bool
  27. }
  28. type Address struct {
  29. gorm.Model
  30. Address1 string
  31. Address2 string
  32. City string
  33. Code string
  34. Country string
  35. }

输出结果为:

  1. go run main.go migrate
  2. migrate called
  3. 2017/08/01 17:10:26 create name<nil>
  4. 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:

  1. db, e := gorm.Open(&quot;postgres&quot;, fmt.Sprintf(&quot;host=%s user=%s dbname=%s password=%s sslmode=disable&quot;, pgHost, pgUser, pgDatabase, pgPass))
  2. if e != nil {
  3. log.Fatal(e.Error())
  4. }
  5. defer db.Close()
  6. db.AutoMigrate(&amp;model.Customer{}, &amp;model.Email{}, &amp;model.Address{}, &amp;model.Name{}, &amp;model.Logindata{})
  7. name := new(model.Name)
  8. email := new(model.Email)
  9. customer := &amp;model.Customer{
  10. Name: name,
  11. PrimaryEmail: email,
  12. }
  13. customer.Name.First = &quot;Darko&quot;
  14. customer.Name.Last = &quot;Luketic&quot;
  15. customer.Name.Middle = &quot;&quot; // also tried without this line
  16. customer.PrimaryEmail.Address = &quot;my@mail.come&quot;
  17. customer.PrimaryEmail.Verified = true
  18. tx := db.Begin()
  19. if e := tx.Create(name); e != nil {
  20. tx.Rollback()
  21. log.Fatal(&quot;create name&quot;, e.Error)
  22. }
  23. if e := tx.Create(email); e != nil {
  24. tx.Rollback()
  25. log.Fatal(&quot;create email&quot;, e.Error)
  26. }
  27. if e := tx.Create(customer); e != nil {
  28. tx.Rollback()
  29. log.Fatal(&quot;create customer&quot;, e.Error)
  30. }
  31. tx.Commit()

with the models being

  1. package model
  2. import &quot;github.com/jinzhu/gorm&quot;
  3. type Customer struct {
  4. gorm.Model
  5. Name *Name
  6. BillingAddress *Address
  7. ShippingAddress *Address
  8. PrimaryEmail *Email
  9. AlternateEmails []*Email
  10. Logindata *Logindata
  11. }
  12. type Name struct {
  13. gorm.Model
  14. First string `json:&quot;first&quot;`
  15. Middle string `json:&quot;middle&quot;`
  16. Last string `json:&quot;last&quot;`
  17. }
  18. type Logindata struct {
  19. gorm.Model
  20. Username string
  21. Password []byte
  22. }
  23. type Email struct {
  24. gorm.Model
  25. Address string
  26. Verified bool
  27. }
  28. type Address struct {
  29. gorm.Model
  30. Address1 string
  31. Address2 string
  32. City string
  33. Code string
  34. Country string
  35. }

The output:

  1. go run main.go migrate
  2. migrate called
  3. 2017/08/01 17:10:26 create name&lt;nil&gt;
  4. exit status 1

What am I doing wrong?
Why is there no error message?
How do I fix it?

答案1

得分: 1

根据文档的说明,Create方法返回的是*DB类型,而不是错误。所以你的代码应该更像这样:

  1. 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

  1. if tx = tx.Create(name); tx.Error != nil { /* ... */ }

答案2

得分: 0

此外,

  1. dbe:= gorm.Open(“ postgres”,fmt.Sprintf(“ host =%s user =%s dbname =%s password =%s sslmode = disable”,pgHostpgUserpgDatabasepgPass))
  2. 如果e!= nil {
  3. log.Fatale.Error())
  4. }
  5. defer db.Close()
  6. db.AutoMigrate(&model.Customer {},&model.Email {},&model.Address {},&model.Name {},&model.Logindata {})
  7. name:= newmodel.Name
  8. email:= newmodel.Email
  9. customer:=&model.Customer {
  10. Namename
  11. PrimaryEmailemail
  12. }
  13. customer.Name.First =“ Darko
  14. customer.Name.Last =“ Luketic
  15. customer.Name.Middle =“”
  16. customer.PrimaryEmail.Address =“ my@mail.come
  17. customer.PrimaryEmail.Verified = true
  18. tx:= db.Begin()
  19. 如果e:= tx.Createcustomer).Error; e!= nil {
  20. tx.Rollback()
  21. log.Fatal(“创建客户”,e
  22. }
  23. 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,

  1. db, e := gorm.Open(&quot;postgres&quot;, fmt.Sprintf(&quot;host=%s user=%s dbname=%s password=%s sslmode=disable&quot;, pgHost, pgUser, pgDatabase, pgPass))
  2. if e != nil {
  3. log.Fatal(e.Error())
  4. }
  5. defer db.Close()
  6. db.AutoMigrate(&amp;model.Customer{}, &amp;model.Email{}, &amp;model.Address{}, &amp;model.Name{}, &amp;model.Logindata{})
  7. name := new(model.Name)
  8. email := new(model.Email)
  9. customer := &amp;model.Customer{
  10. Name: name,
  11. PrimaryEmail: email,
  12. }
  13. customer.Name.First = &quot;Darko&quot;
  14. customer.Name.Last = &quot;Luketic&quot;
  15. customer.Name.Middle = &quot;&quot;
  16. customer.PrimaryEmail.Address = &quot;my@mail.come&quot;
  17. customer.PrimaryEmail.Verified = true
  18. tx := db.Begin()
  19. if e := tx.Create(customer).Error; e != nil {
  20. tx.Rollback()
  21. log.Fatal(&quot;create customer&quot;, e)
  22. }
  23. 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 go-gorm,postgres:简单插入返回错误

  1. package model
  2. import &quot;github.com/jinzhu/gorm&quot;
  3. type Customer struct {
  4. gorm.Model
  5. Name *Name
  6. BillingAddress *Address
  7. ShippingAddress *Address
  8. PrimaryEmail *Email
  9. AlternateEmails []*Email
  10. Logindata *Logindata
  11. }
  12. type Name struct {
  13. gorm.Model
  14. CustomerID uint
  15. First string `json:&quot;first&quot;`
  16. Middle string `json:&quot;middle&quot;`
  17. Last string `json:&quot;last&quot;`
  18. }
  19. type Logindata struct {
  20. gorm.Model
  21. CustomerID uint
  22. Username string
  23. Password []byte
  24. }
  25. type Email struct {
  26. gorm.Model
  27. CustomerID uint
  28. Address string
  29. Verified bool
  30. }
  31. type Address struct {
  32. gorm.Model
  33. CustomerID uint
  34. Address1 string
  35. Address2 string
  36. City string
  37. Code string
  38. Country string
  39. }

note the CustomerID uint

huangapple
  • 本文由 发表于 2017年8月1日 23:22:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/45441783.html
匿名

发表评论

匿名网友

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

确定