dongpo5207 2015-12-04 12:07
浏览 389
已采纳

gorm忽略sql:“ index”标签

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.

  • 写回答

1条回答 默认 最新

  • dongliang1865 2015-12-04 16:05
    关注

    From my experience, the db.CreateTable only creates the table and it's fields. You are better off using the AutoMigrate function with the model structure that you want to migrate:

    db, err := gorm.Open("postgres", connectionString)
    ...
    // error checking
    ...
    
    db.AutoMigrate(&Model)
    

    Also, I tried AutoMigrating the model you posted and got an error saying that multiple primary keys are not allowed, so I changed the model to:

    type Model struct {
        Id        int64 `sql:"type:bigint;default:0"`
        CreatedAt time.Time
        UpdatedAt time.Time
        DeletedAt *time.Time `sql:"index"`
    }
    

    and the AutoMigration created all PKs and indexes just fine.

    Edit:

    Checking the GORM's README, on this example, the Email structure goes as:

    type Email struct {
        ID      int
        UserID  int     `sql:"index"` // Foreign key (belongs to), tag `index` will create index for this field when using AutoMigrate
        Email   string  `sql:"type:varchar(100);unique_index"` // Set field's sql type, tag `unique_index` will create unique index
        Subscribed bool
    }
    

    Notice the comment on the UserId field saying it will create the index when using AutoMigrate.

    Also, it's worth taking a look at how the AutoMigrate does it's job:

    // Automating Migration
    db.AutoMigrate(&User{})
    db.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(&User{})
    db.AutoMigrate(&User{}, &Product{}, &Order{})
    // Feel free to change your struct, AutoMigrate will keep your database up-to-date.
    // AutoMigrate will ONLY add *new columns* and *new indexes*,
    // WON'T update current column's type or delete unused columns, to protect your data.
    // If the table is not existing, AutoMigrate will create the table automatically.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题