Your Question

我的表结构如下

type Location struct {
    Id        int    `gorm:"primaryKey,autoIncrement,comment:地理位置表的自增id,<-:create" json:"id,omitempty"`
    ClassId   string `gorm:"index:location_classId_type_index,size:50,comment:类型对应的数据id\,可能是项目id或者活动id,not null" json:"ClassId,omitempty"`
    Province  string `gorm:"size:50,comment:地理位置省" json:"province,omitempty"` //省
    City      string `gorm:"size:50,comment:地理位置市" json:"city,omitempty"`     //市
    County    string `gorm:"size:50,comment:地理位置县" json:"county,omitempty"`   //县
    ClassType int    `gorm:"index:location_classId_type_index,comment:id类型\,0代表项目. 1代表活动" json:"classType,omitempty"`
}

代码如下

    err = db.AutoMigrate(&proj.Project{})
    if err != nil {
        log.Error("创建数据库失败: " + err.Error())
    }

没有报错 但是生成的表结构如下

CREATE TABLE `locations` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `class_id` longtext,
  `province` longtext,
  `city` longtext,
  `county` longtext,
  `class_type` bigint DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

图如下 Gorm gorm使用 AutoMigrate ,联合索引创建失败

我按照文档的说明在ClassIdClassType都添加了index:location_classId_type_index 但是却没有生成联合索引

The document you expected this should be explained

Expected answer

期望生成联合索引

Comment From: ChinaRedArmy1930

是我搞错了 应该使用分号 ; 分割,我使用的逗号分割tag 改成分号就OK了