What version of Go are you using (go version)?
1.9
Which database and its version are you using?
pg 9.5.3
I would like to be able to specify the start value of the generated sequence for an overridden primary key field definition. I would like the generated sequence behind the primary_key directive to create something like this (i.e. start == 10000000 rather than 1).
CREATE SEQUENCE public.equipment_equipment_num_seq
INCREMENT 1
START 10000000
MINVALUE 1
MAXVALUE 9223372036854775807
CACHE 1;
ALTER SEQUENCE public.equipment_equipment_num_seq
OWNER TO godev;
package main
import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mssql"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/postgres"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
var db *gorm.DB
// Equipment struct has a time-sliced primary key; EquipmentNum needs a mask
// (starting-point) of 1000000000.
// I saw this: MyiD uint64 `sql:"type:integer PRIMARY KEY;default:user_gen_id()"
// but would hope that it is possible to provide the start value in the gorm: directive?
type Equipment struct {
EquipmentNum int64 `gorm:"primary_key"`
ValidFrom time.Time `gorm:"primary_key"`
ValidTo time.Time `gorm:"primary_key"`
CreatedAt time.Time
MaterialNum int64
Description string
Serial string
}
func init() {
var err error
// db, err = gorm.Open("sqlite3", "test.db")
// Please use below username, password as your database's account for the script.
/db, err = gorm.Open("postgres", "user=gorm dbname=gorm sslmode=disable")
// db, err = gorm.Open("mysql", "gorm:gorm@/dbname?charset=utf8&parseTime=True")
// db, err = gorm.Open("mssql", "sqlserver://gorm:LoremIpsum86@localhost:1433?database=gorm")
if err != nil {
panic(err)
}
db.LogMode(true)
}
func main() {
// for pg, I could alter the sequence manually, but I am using gorm to avoid
// coding to a specific db...
if db.HasTable(Equipment{}) == false {
db.CreateTable(Equipment{})
db.Exec("ALTER SEQUENCE equipment_equipment_num_seq RESTART WITH 10000000")
} else {
db.AutoMigrate(Equipment{})
}
db.Close()
}
thanks,
steve
Comment From: jinzhu
Don't support this, maybe use SQL to do advanced requirements
Comment From: newproplus
I have the same question.After my program executed,the "SEQUENCE" does not do as "AUTO_INCREMENT" go: 1.19 pgsql: 14 gorm v1.23.8 postgres v1.3.8