Error 1215: Cannot add foreign key constraint

来源:1-6 生成表结构和导入数据

香饽饽0

2021-08-11 15:45:58

[12.317ms] [rows:0] CREATE TABLE `goods` (`id` bigint unsigned AUTO_INCREMENT,`created_at` datetime(3) NULL,`updated_at` datetime(3) NULL,`deleted_at` datetime(3) NULL,`category_id` int unsigned NOT NULL,`brand_id` int unsigned NOT NULL,`on_sale` boolean NOT NULL DEFAULT false,`ship_free` boolean NOT NULL DEFAULT false,`is_new` boolean NOT NULL DEFAULT false,`is_hot` boolean NOT NULL DEFAULT false,`name` varchar(50) NOT NULL,`goods_sn` varchar(50) NOT NULL,`click_num` int NOT NULL DEFAULT 0,`sold_num` int NOT NULL DEFAULT 0,`fav_num` int NOT NULL DEFAULT 0,`market_price` float NOT NULL,`shop_price` float NOT NULL,`goods_brief` longtext NOT NULL,`images` longblob NOT NULL,`desc_images` longblob NOT NULL,`goods_front_image` varchar(200) NOT NULL,PRIMARY KEY (`id`),INDEX idx_goods_deleted_at (`deleted_at`),CONSTRAINT `fk_goods_category` FOREIGN KEY (`category_id`) REFERENCES `category`(`id`),CONSTRAINT `fk_goods_brand` FOREIGN KEY (`brand_id`) REFERENCES `brand`(`id`))


```golang


type Category struct {
gorm.Model
  Name             string `gorm:"type:varchar(20);not null"`
  Level            int32  `gorm:"type:int;not null;default:1"`
  IsTab            bool   `gorm:"default:false;not null"`
  ParentCategoryID int32
  ParentCategory   *Category
  Brand            []*Brand `gorm:"many2many:category_brand"`
}

type Brand struct {
gorm.Model
  Name     string      `gorm:"type:varchar(20);not null"`
  Logo     string      `gorm:"type:varchar(200);default:'';not null"`
  Category []*Category `gorm:"many2many:category_brand;"`
}

type Banner struct {
gorm.Model
  Image string `gorm:"type:varchar(200);not null"`
  Url   string `gorm:"type:varchar(200);not null"`
  Index int32  `gorm:"type:int;default:1;not null"`
}

type Goods struct {
gorm.Model

  CategoryID int32 `gorm:"type:int;not null"`
  Category   Category

  BrandID int32 `gorm:"type:int;not null"`
  Brand   Brand

  OnSale   bool `gorm:"default:false;not null"`
  ShipFree bool `gorm:"default:false;not null"`
  IsNew    bool `gorm:"default:false;not null"`
  IsHot    bool `gorm:"default:false;not null"`

  Name            string   `gorm:"type:varchar(50);not null"`
  GoodsSn         string   `gorm:"type:varchar(50);not null"`
  ClickNum        int32    `gorm:"type:int;default:0;not null"`
  SoldNum         int32    `gorm:"type:int;default:0;not null"`
  FavNum          int32    `gorm:"type:int;default:0;not null"`
  MarketPrice     float32  `gorm:"not null"`
  ShopPrice       float32  `gorm:"not null"`
  GoodsBrief      string   `gorm:"type varchar(100);not null"`
  Images          GormList `gorm:"type varchar(1000);not null"`
  DescImages      GormList `gorm:"type varchar(1000);not null"`
  GoodsFrontImage string   `gorm:"type:varchar(200);not null"`
}

```

写回答

1回答

bobby

2021-08-12

BrandID int32 `gorm:"type:int;not null"`
Brand Brand

这是外键类型不一致造成的,你的外键是brand的id字段,但是id是uint类型,但是你这里的

BrandID 

是int32类型,所以需要类型统一

BrandID uint `gorm:"type:int;not null"`
Brand Brand


0

0 学习 · 1399 问题

查看课程

相似问题

回答 1

回答 1