Gorm: auto increment doesn't work

Created on 23 Nov 2018  路  8Comments  路  Source: go-gorm/gorm

Your issue may already be reported! Please search on the issue track before creating one.

What version of Go are you using (go version)?

go1.11.1

Which database and its version are you using?

mysql 8.0.13

package main

import (
    "fmt"
)

import (
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/mysql"
)

type TParkRecord struct {
    ID       uint `gorm:"type:bigint(20) unsigned;not null;auto_increment;primary_key"`
    ParkCode int  `gorm:"type:bigint(10) unsigned;not null;default:'0'"`
}

func (t TParkRecord) TableName() string {
    return "t_park_record"
}

func main() {
    db, err := gorm.Open("mysql", "user:password@/dbname?charset=utf8&parseTime=True&loc=Local")
    if err != nil {
        fmt.Println(err)
    }
    defer db.Close()

    db.LogMode(true)

    db.Set("gorm:table_options", "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE = utf8_bin").AutoMigrate(&TParkRecord{})
}

when i run this code,i found auto increment doesn't work.

image

Most helpful comment

@onecaffeine
璇峰儚杩欐牱瀹氫箟瀛楁

ID uint64 `gorm:"type:bigint(20) unsigned auto_increment;not null;primary_key"`

All 8 comments

@jinzhu
please tell me what should i do?thx

@onecaffeine
璇峰儚杩欐牱瀹氫箟瀛楁

ID uint64 `gorm:"type:bigint(20) unsigned auto_increment;not null;primary_key"`

@baocaixiong
it works!!
why AUTO_INCREMENT is special for bigint ??

Seems this comment clarifies:
https://github.com/jinzhu/gorm/issues/201#issuecomment-52582868

@inliquid Thanks. I get it.

@baocaixiong thx,it works!!

For anyone else having issues, setting the type to serial worked for me.
``go type User struct { ID intgorm:"type: serial;"`
}
````
This sets the column default to the next sequential value:
"nextval('users_account_number_seq'::regclass)"

For anyone else having issues, setting the type to serial worked for me.

type User struct {
      ID int `gorm:"type: serial;"`
}

This sets the column default to the next sequential value:
"nextval('users_account_number_seq'::regclass)"

Works for me too!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

koalacxr picture koalacxr  路  3Comments

littletwolee picture littletwolee  路  3Comments

rfyiamcool picture rfyiamcool  路  3Comments

Quentin-M picture Quentin-M  路  3Comments

easonlin404 picture easonlin404  路  3Comments