This is the 23rd day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
Writing in the front
In the first section, we determine the ER graph, the data dictionary. In section 2, we’ve configured MySQL. Combining the previous two chapters, we are ready to create database tables.
1. Initialize the connection
- init.go
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) / / GORM print
iferr ! =nil {
panic(err)
}
if gin.Mode() == "release" {
db.LogMode(false)
}
db.SingularTable(true) // Do not add complex s by default
db.DB().SetMaxIdleConns(20) // Set the connection pool to idle
db.DB().SetMaxOpenConns(100) / / open
db.DB().SetConnMaxLifetime(time.Second * 30)
DB = db
migration()
}
Copy the code
2. The table is established
- user
The users table
type User struct {
gorm.Model
UserName string `gorm:"unique"`
Email string //`gorm:"unique"`
PasswordDigest string
Nickname string `gorm:"not null"`
Status string
Avatar string `gorm:"size:1000"`
Money int
}
Copy the code
- The product goods table
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
}
Copy the code
- ProductImg Product picture list
type ProductImg struct {
gorm.Model
Product Product `gorm:"ForeignKey:ProductID"`
ProductID uint `gorm:"not null"`
ImgPath string
}
Copy the code
- ProductParamImg Commodity parameter list
type ProductParamImg struct {
gorm.Model
Product Product `gorm:"ForeignKey:ProductID"`
ProductID uint `gorm:"not null"`
ImgPath string
}
Copy the code
- ProductInfoImg Product details table
type ProductInfoImg struct {
gorm.Model
Product Product `gorm:"ForeignKey:ProductID"`
ProductID uint `gorm:"not null"`
ImgPath string
}
Copy the code
- The cart table
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
}
Copy the code
- Order the Order sheet
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
}
Copy the code
- Address table
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"`
}
Copy the code
- Collect the table
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"`
}
Copy the code
- Classification table
type Category struct {
gorm.Model
CategoryName string
}
Copy the code
- The administrator table
type Admin struct {
gorm.Model
UserName string
PasswordDigest string
Avatar string `gorm:"size:1000"`
}
Copy the code
- By the chart
type Carousel struct {
gorm.Model
ImgPath string
Product Product `gorm:"ForeignKey:ProductID"`
ProductID uint `gorm:"not null"`
}
Copy the code
3. The migration
- Do the migration and apply the foreign key constraint
func migration(a) {
Automatic migration mode
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")}Copy the code
AutoMigrate
The function maps code to a databaseAddForeignKey
The function is to add a foreign key
For example,
DB.Model(&Cart{}).AddForeignKey("product_id"."Product(id)"."CASCADE"."CASCADE")
Copy the code
This function associates the product_ID field with the ID of the product in the Cart table. The last two are cascading updates or deletes during update and DELETE.