Gorm sets foreign key constraints

two tables, one is category, and the other is product, defined as follows:

type Product struct {
    ProductId  uint32 `gorm:"primary_key;type:int(11) unsigned auto_increment;"`
    Category Category `gorm:"ForeignKey:CateIDRef;"`  //category
    CateIDRef uint16 `gorm:"type:mediumint(11) unsigned not null;"`
    //CateTitle string `gorm:"type:varchar(30);not null;"`
    //Title string `gorm:"type:varchar(30)  not null;"`
    Summary string `gorm:"type:varchar(255)  not null;"`
    Num  uint32  `gorm:"type:int unsigned  not null; default:0;"`
    Price float64 `gorm:"type:decimal(10,2) unsigned not null;"`
    Pics string        `gorm:"type:varchar(500); default: "";"`
    CreateTime time.Time `gorm:"type:timestamp not null;default:current_timestamp;"`
    ModifyTime time.Time `gorm:"type:timestamp on update current_timestamp;omitempty;default:current_timestamp;"`
}

type Category struct {
    CateID uint16  `gorm:"primary_key;type:mediumint(11) not null unsigned auto_increment;"`
    ParentID uint16  `gorm:"type:mediumint(11) unsigned;not null;"`
    Title string `gorm:"type:varchar(30);not null;unique"`
    CreateTime time.Time `gorm:"type:timestamp;not null;default:current_timestamp;"`
    ModifyTime time.Time `gorm:"type:timestamp;not null on update current_timestamp;default:current_timestamp;"`
    Level uint16 `gorm:"-"`
}

but the creation of the foreign key is not successful. I don"t know the specific reason.

Jan.29,2022

db.Model(&Product{}).AddForeignKey()

in addition, these two tables do not need to set the primary foreign key association

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e3dacb-58cd8.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e3dacb-58cd8.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?