How to add auto increment to Go struct with json

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

How to add auto increment to Go struct with json

问题

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

package article

import "github.com/jinzhu/gorm"

type Article struct {
    gorm.Model
    Id          int    `json:"id"`
    Title       string `json:"title"`
    Description string `json:"description"`
    Content     string `json:"content"`
}

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

`gorm:"default:'galeone'"`

但是它无法编译通过。

package article

import "github.com/jinzhu/gorm"

type Article struct {
    gorm.Model
    Id          uint    `json:"id" sql:"AUTO_INCREMENT" gorm:"primary_key"`
    Title       string `json:"title"`
    Description string `json:"description"`
    Content     string `json:"content"`
}

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

我得到了以下错误:

2016/12/21 15:17:48 DB Initialized successfully

(duplicate column name: id) 
[2016-12-21 15:17:48]  

(no such table: articles) 
[2016-12-21 15:17:48]  

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

package dbprovider

import (
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/sqlite"
    "rest/article"
    "log"
)

var db gorm.DB

var isInitialized bool

func InitDb() {
    isInitialized = false
    db, err := gorm.Open("sqlite3", "../../articles.db")

    if (db != nil && err == nil) {
        log.Print("Db Initialized")
        isInitialized = true
    } else {
        isInitialized = false
        defer db.Close()
        log.Panic("DB not initialized")
    }
}

func AddArticle(article *article.Article) {
    if (isInitialized) {
        db.Create(&article)
    }
}
英文:

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

package article

import "github.com/jinzhu/gorm"

type Article struct {
	gorm.Model
	Id          int    `json:"id"`
	Title       string `json:"title"`
	Description string `json:"description"`
	Content     string `json:"content"`
}

I wish to add gorm property like this

`gorm:"default:'galeone'"`

But it is not compiling

package article

import "github.com/jinzhu/gorm"

type Article struct {
	gorm.Model
	Id          uint    `json:"id" sql:"AUTO_INCREMENT" gorm:"primary_key"`
	Title       string `json:"title"`
	Description string `json:"description"`
	Content     string `json:"content"`
}

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

I am getting

2016/12/21 15:17:48 DB Initialized successfully

(duplicate column name: id) 
[2016-12-21 15:17:48]  

(no such table: articles) 
[2016-12-21 15:17:48]  

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

package dbprovider

import (
	"github.com/jinzhu/gorm"
	_"github.com/jinzhu/gorm/dialects/sqlite"
	"rest/article"
	"log"
)

var db gorm.DB

var isInitialized bool

func InitDb() {
	isInitialized = false
	db, err := gorm.Open("sqlite3", "../../articles.db")

	if (db != nil && err == nil) {
		log.Print("Db Initialized")
		isInitialized = true
	} else {
		isInitialized = false
		defer db.Close()
		log.Panic("DB not initialized")
	}
}

func AddArticle(article *article.Article) {
	if (isInitialized) {
		db.Create(&article)
	}
}

答案1

得分: 1

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

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

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

  1. var db gorm.DB -> var db *gorm.DB
  2. func InitDb
func InitDb() {
    isInitialized = false
    //Change below code 
    var err interface{}
    db, err = gorm.Open("sqlite3", "../../articles.db")

    if db != nil && err == nil {
        log.Print("Db Initialized")
        isInitialized = true
    } else {
        isInitialized = false
        defer db.Close()
        log.Panic("DB not initialized")
    }
}
英文:

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

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
func InitDb() {
isInitialized = false
//Change below code 
var err interface{}
db, err = gorm.Open("sqlite3", "../../articles.db")

if (db != nil && err == nil) {
    log.Print("Db Initialized")
    isInitialized = true
} else {
    isInitialized = false
    defer db.Close()
    log.Panic("DB not initialized")
}

}

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:

确定