账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    gorm设置外键约束
    49
    0

    两张表,一张是category,一张product,定义如下:

    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:"-"`
    }

    但是外键创建不成功,不知道具体原因在哪儿,求解答。

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • Pasttense 普通会员 1楼
      502 Bad Gateway

      502 Bad Gateway


      nginx
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部