写在前面
第一节,我们确定了ER图 ,数据字典 。 第二节,我们已经配置了MySQL 。 结合前两章,我们就可以建立数据库表了。
1. 初始化连接
package model
import (
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"time"
)
var DB *gorm.DB
func Database(connString string) {
db, err := gorm.Open("mysql", connString)
db.LogMode(true)
if err != nil {
panic(err)
}
if gin.Mode() == "release" {
db.LogMode(false)
}
db.SingularTable(true)
db.DB().SetMaxIdleConns(20)
db.DB().SetMaxOpenConns(100)
db.DB().SetConnMaxLifetime(time.Second * 30)
DB = db
migration()
}
2. 建立表
用户表
type User struct {
gorm.Model
UserName string `gorm:"unique"`
Email string
PasswordDigest string
Nickname string `gorm:"not null"`
Status string
Avatar string `gorm:"size:1000"`
Money int
}
type Product struct {
gorm.Model
Name string `gorm:"size:255;index"`
Category Category `gorm:"ForeignKey:CategoryID"`
CategoryID uint `gorm:"not null"`
Title string
Info string `gorm:"size:1000"`
ImgPath string
Price string
DiscountPrice string
OnSale bool `gorm:"default:false"`
Num int
BossID int
BossName string
BossAvatar string
}
type ProductImg struct {
gorm.Model
Product Product `gorm:"ForeignKey:ProductID"`
ProductID uint `gorm:"not null"`
ImgPath string
}
type ProductParamImg struct {
gorm.Model
Product Product `gorm:"ForeignKey:ProductID"`
ProductID uint `gorm:"not null"`
ImgPath string
}
type ProductInfoImg struct {
gorm.Model
Product Product `gorm:"ForeignKey:ProductID"`
ProductID uint `gorm:"not null"`
ImgPath string
}
type Cart struct {
gorm.Model
UserID uint
Product Product `gorm:"ForeignKey:ProductID"`
ProductID uint `gorm:"not null"`
BossID uint
Num uint
MaxNum uint
Check bool
}
type Order struct {
gorm.Model
User User `gorm:"ForeignKey:UserID"`
UserID uint `gorm:"not null"`
Product Product `gorm:"ForeignKey:ProductID"`
ProductID uint `gorm:"not null"`
Boss User `gorm:"ForeignKey:BossID"`
BossID uint `gorm:"not null"`
Address Address `gorm:"ForeignKey:AddressID"`
AddressID uint `gorm:"not null"`
Num uint
OrderNum uint64
Type uint
Money int
}
type Address struct {
gorm.Model
User User `gorm:"ForeignKey:UserID"`
UserID uint `gorm:"not null"`
Name string `gorm:"type:varchar(20) not null"`
Phone string `gorm:"type:varchar(11) not null"`
Address string `gorm:"type:varchar(50) not null"`
}
type Favorite struct {
gorm.Model
User User `gorm:"ForeignKey:UserID"`
UserID uint `gorm:"not null"`
Product Product `gorm:"ForeignKey:ProductID"`
ProductID uint `gorm:"not null"`
Boss User `gorm:"ForeignKey:BossID"`
BossID uint `gorm:"not null"`
}
type Category struct {
gorm.Model
CategoryName string
}
type Admin struct {
gorm.Model
UserName string
PasswordDigest string
Avatar string `gorm:"size:1000"`
}
type Carousel struct {
gorm.Model
ImgPath string
Product Product `gorm:"ForeignKey:ProductID"`
ProductID uint `gorm:"not null"`
}
3. 迁移
func migration() {
DB.Set("gorm:table_options", "charset=utf8mb4").
AutoMigrate(&User{}).
AutoMigrate(&Product{}).
AutoMigrate(&Carousel{}).
AutoMigrate(&Category{}).
AutoMigrate(&Favorite{}).
AutoMigrate(&ProductImg{}).
AutoMigrate(&ProductInfoImg{}).
AutoMigrate(&ProductParamImg{}).
AutoMigrate(&Order{}).
AutoMigrate(&Cart{}).
AutoMigrate(&Admin{}).
AutoMigrate(&Address{})
DB.Model(&Cart{}).AddForeignKey("product_id","Product(id)","CASCADE","CASCADE")
DB.Model(&Order{}).AddForeignKey("user_id","User(id)","CASCADE","CASCADE")
DB.Model(&Order{}).AddForeignKey("address_id","Address(id)","CASCADE","CASCADE")
DB.Model(&Order{}).AddForeignKey("product_id","Product(id)","CASCADE","CASCADE")
DB.Model(&Order{}).AddForeignKey("boss_id","User(id)","CASCADE","CASCADE")
DB.Model(&Favorite{}).AddForeignKey("boss_id","User(id)","CASCADE","CASCADE")
DB.Model(&Favorite{}).AddForeignKey("user_id","User(id)","CASCADE","CASCADE")
DB.Model(&Favorite{}).AddForeignKey("product_id","Product(id)","CASCADE","CASCADE")
DB.Model(&Product{}).AddForeignKey("category_id","Category(id)","CASCADE","CASCADE")
DB.Model(&ProductImg{}).AddForeignKey("product_id","Product(id)","CASCADE","CASCADE")
DB.Model(&ProductInfoImg{}).AddForeignKey("product_id","Product(id)","CASCADE","CASCADE")
DB.Model(&ProductParamImg{}).AddForeignKey("product_id","Product(id)","CASCADE","CASCADE")
DB.Model(&Address{}).AddForeignKey("user_id","User(id)","CASCADE","CASCADE")
}
AutoMigrate 函数是把代码映射到数据库中AddForeignKey 函数是添加外键
举例子
DB.Model(&Cart{}).AddForeignKey("product_id","Product(id)","CASCADE","CASCADE")
这个函数就是在Cart 表中把product_id 字段关联到product 的id 中,后面两个就是update 和delete 的时候进行级联更新 或是级联删除 。
|