Why gorm
is ignoring sql:"index"
tags? No indexes got created.
Database in use here is PostgreSQL (importing _ "github.com/lib/pq"
). This Model
struct is used (because default gorm.Model
uses an auto increment number - serial
- as primary key and I wanted to set id
myself):
type Model struct {
ID int64 `sql:"type:bigint PRIMARY KEY;default:0"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time `sql:"index"`
}
And one of actual models is:
type TUHistory struct {
Model
TUID int64 `json:"tu_id,string" gorm:"column:tu_id" sql:"index"`
}
func (x *TUHistory) TableName() string {
return "tu_history"
}
And the table is created by db.CreateTable(&TUHistory{})
which creates the table correctly except for indexes.
As a temporary work around, I do db.Model(&TUHistory{}).AddIndex("ix_tuh_tu_id", "tu_id")
to create indexes.