Your Question

How to use two fields to union unique ??

The document you expected this should be explained

Eg:


type Role struct {
    ID             string         `gorm:"id;primaryKey"`  
    Name           string          
    Alias          string               
}

Expected answer

After inserting values (name : test , alias : test ) successful, if we insert same values (name : test , alias : test ) again, the db should return conflicted

The two fields name with alias should unique_together like Django Orm

Comment From: s-takehana

Try this:

type Role struct {
    ID    string `gorm:"id;primaryKey"`
    Name  string `gorm:"index:idx_name,unique"`
    Alias string `gorm:"index:idx_name,unique"`
}

https://gorm.io/docs/indexes.html

Comment From: DasyDong

Cool Man ! @s-takehana i took mine by mistake this gorm:"index:idx_name;unique" , actually is gorm:"index:idx_name,unique" . HaHa

Comment From: serious-snow

thanks.You helped me a lot.