1

I was wondering if it would be possible to create an index using a primitive field and the fields of another struct inside a third struct.

I have a YearMonth struct that has some convenient methods I use across my API I also use this struct to provide the year and month fields for my other structs that need it. and to create this index on other tables as well.

type YearMonth struct {
    Year  int        `uri:"year" gorm:"index:,unique,composite:year_month,priority:1" json:"Year"`
    Month time.Month `uri:"month" gorm:"index:,unique,composite:year_month,priority:2" json:"Month"`
}

I bumped into a different case where I would need to add another field to complete my unique constraint

type CloudfrontData struct {
    YearMonth        `gorm:"index:,unique,composite:year_month_account,priority:1"`
    AWSAccount       string `gorm:"index:,unique,composite:year_month_account,priority:2"`
    BytesTransferred float64
    ModelWithoutID
}

Is there a way to create the index year_month_account without creating the year_month index and by using the YearMonth struct? I would like to keep reusing YearMonth and it's convenient methods without having to replicate them.

Thanks in advance

0 Answers0