Example #1
0
func compositeIndex(table *gorm.DB, kind string, primary_key string, dst interface{}, parents []string) {

	index_name := "idx_" + kind + "_" + primary_key
	indexes := []string{primary_key}

	if len(parents) == 0 {
		parents = []string{}
		st := reflect.TypeOf(dst).Elem()
		for i := 0; i < st.NumField(); i++ {
			field := st.Field(i)
			pacific_parent := field.Tag.Get("pacific_parent")
			if pacific_parent != "" {
				parents = append(parents, pacific_parent)
			}
		}
	}

	for _, parent := range parents {
		if parent != "" {
			index_name = index_name + "_" + parent
			indexes = append(indexes, parent)
		}
	}

	if len(indexes) > 0 {
		table.AddUniqueIndex(index_name, indexes...) //TODO: if not exists?
	}
}