How to add auto increment to Go struct with json

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

How to add auto increment to Go struct with json

问题

我想在GORM中覆盖Id值字段,因为我还使用json进行编组和解组。

  1. package article
  2. import "github.com/jinzhu/gorm"
  3. type Article struct {
  4. gorm.Model
  5. Id int `json:"id"`
  6. Title string `json:"title"`
  7. Description string `json:"description"`
  8. Content string `json:"content"`
  9. }

我希望像这样添加gorm属性:

  1. `gorm:"default:'galeone'"`

但是它无法编译通过。

  1. package article
  2. import "github.com/jinzhu/gorm"
  3. type Article struct {
  4. gorm.Model
  5. Id uint `json:"id" sql:"AUTO_INCREMENT" gorm:"primary_key"`
  6. Title string `json:"title"`
  7. Description string `json:"description"`
  8. Content string `json:"content"`
  9. }

我正在使用来自这里的Gorm [https://github.com/jinzhu/gorm][1]

我得到了以下错误:

  1. 2016/12/21 15:17:48 DB Initialized successfully
  2. (duplicate column name: id)
  3. [2016-12-21 15:17:48]
  4. (no such table: articles)
  5. [2016-12-21 15:17:48]

这是我创建数据库的方式,它工作正常,只是想在Article结构上自动递增。

  1. package dbprovider
  2. import (
  3. "github.com/jinzhu/gorm"
  4. _ "github.com/jinzhu/gorm/dialects/sqlite"
  5. "rest/article"
  6. "log"
  7. )
  8. var db gorm.DB
  9. var isInitialized bool
  10. func InitDb() {
  11. isInitialized = false
  12. db, err := gorm.Open("sqlite3", "../../articles.db")
  13. if (db != nil && err == nil) {
  14. log.Print("Db Initialized")
  15. isInitialized = true
  16. } else {
  17. isInitialized = false
  18. defer db.Close()
  19. log.Panic("DB not initialized")
  20. }
  21. }
  22. func AddArticle(article *article.Article) {
  23. if (isInitialized) {
  24. db.Create(&article)
  25. }
  26. }
英文:

I want to override Id value field in GORM with since I am using json also to marchal and unmarshall.

  1. package article
  2. import "github.com/jinzhu/gorm"
  3. type Article struct {
  4. gorm.Model
  5. Id int `json:"id"`
  6. Title string `json:"title"`
  7. Description string `json:"description"`
  8. Content string `json:"content"`
  9. }

I wish to add gorm property like this

  1. `gorm:"default:'galeone'"`

But it is not compiling

  1. package article
  2. import "github.com/jinzhu/gorm"
  3. type Article struct {
  4. gorm.Model
  5. Id uint `json:"id" sql:"AUTO_INCREMENT" gorm:"primary_key"`
  6. Title string `json:"title"`
  7. Description string `json:"description"`
  8. Content string `json:"content"`
  9. }

I am using Gorm from here [https://github.com/jinzhu/gorm][1]

I am getting

  1. 2016/12/21 15:17:48 DB Initialized successfully
  2. (duplicate column name: id)
  3. [2016-12-21 15:17:48]
  4. (no such table: articles)
  5. [2016-12-21 15:17:48]

This is how i am creating DB it is working fine just want auto increment on Article struct

  1. package dbprovider
  2. import (
  3. "github.com/jinzhu/gorm"
  4. _"github.com/jinzhu/gorm/dialects/sqlite"
  5. "rest/article"
  6. "log"
  7. )
  8. var db gorm.DB
  9. var isInitialized bool
  10. func InitDb() {
  11. isInitialized = false
  12. db, err := gorm.Open("sqlite3", "../../articles.db")
  13. if (db != nil && err == nil) {
  14. log.Print("Db Initialized")
  15. isInitialized = true
  16. } else {
  17. isInitialized = false
  18. defer db.Close()
  19. log.Panic("DB not initialized")
  20. }
  21. }
  22. func AddArticle(article *article.Article) {
  23. if (isInitialized) {
  24. db.Create(&article)
  25. }
  26. }

答案1

得分: 1

首先,根据办公室指南,gorm:"default:'galeone'"是你的字段的默认值。当你没有给出值时,参考gormDefaultValue。所以你的ID字段需要更改,因为你的默认值是字符串,但字段是整数。

  1. Id int `json:"id" gorm:"default:1"`

InitDb函数中,你重新定义了一个变量db。当你编译或运行Go程序时,会出现错误。你需要更改两行代码:

  1. var db gorm.DB -> var db *gorm.DB
  2. func InitDb
  1. func InitDb() {
  2. isInitialized = false
  3. //Change below code
  4. var err interface{}
  5. db, err = gorm.Open("sqlite3", "../../articles.db")
  6. if db != nil && err == nil {
  7. log.Print("Db Initialized")
  8. isInitialized = true
  9. } else {
  10. isInitialized = false
  11. defer db.Close()
  12. log.Panic("DB not initialized")
  13. }
  14. }
英文:

First. According to Office Guideline
gorm:"default:'galeone'" is your field default value

Refer : gormDefaultValue
when you not give the value.
so your ID field need to be change . because your default value is string
but the field is int

  1. Id int `json:"id" gorm:"default:1"`

and on func InitDb. you redefinition a variable db.Will occur error when you compiler or run go program. you need change two line

  1. var db gorm.DB -> var db *gorm.DB
  2. func InitDb
  1. func InitDb() {
  2. isInitialized = false
  3. //Change below code
  4. var err interface{}
  5. db, err = gorm.Open("sqlite3", "../../articles.db")
  6. if (db != nil && err == nil) {
  7. log.Print("Db Initialized")
  8. isInitialized = true
  9. } else {
  10. isInitialized = false
  11. defer db.Close()
  12. log.Panic("DB not initialized")
  13. }

}

huangapple
  • 本文由 发表于 2016年12月21日 15:41:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/41257525.html
匿名

发表评论

匿名网友

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

确定