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 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)