You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
3.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package gormExample
import (
"gorm.io/gorm"
"gorm.io/gorm/clause"
"log"
"time"
)
type Content struct {
gorm.Model
Subject string
Likes uint `gorm:""`
Views uint `gorm:""`
//Likes uint `gorm:"default:99"`
//Views *uint `gorm:"default:99"`
PublishTime *time.Time
// 不需要迁移
// 禁用写操作
Sv string `gorm:"-:migration;<-:false"`
// 作者ID
AuthorID uint
}
//// Author模型
//type Author struct {
// gorm.Model
// Status int
//
// Name string
// Email string
//}
type ContentStrPK struct {
ID string `gorm:"primaryKey"`
Subject string
Likes uint
Views uint
PublishTime *time.Time
}
const (
defaultViews = 99
defaultLikes = 99
)
func NewContent() Content {
return Content{
Likes: defaultLikes,
Views: defaultViews,
}
}
// Hook
func (c *Content) BeforeCreate(db *gorm.DB) error {
// 业务
if c.PublishTime == nil {
now := time.Now()
c.PublishTime = &now
}
// 配置
db.Statement.AddClause(clause.OnConflict{UpdateAll: true})
log.Println("content before create hook")
return nil
}
func (c *Content) AfterCreate(db *gorm.DB) error {
//return errors.New("custom error")
return nil
}
func (c *Content) AfterFind(db *gorm.DB) error {
if c.AuthorID == 0 {
c.AuthorID = 1 // 1 假定的默认作者
}
return nil
}
// 作者模型
// Author模型
type Author struct {
gorm.Model
Status int
Name string
Email string
// 积分
Points int
// 拥有多个论文内容
// has many
// 默认关联
Essays []Essay `gorm:"constraint:OnDelete:SET NULL;"`
// 第一作者论文列表
FirstEssays []Essay `gorm:"foreignKey:FirstAuthorID;references:;"`
// 第二作者论文列表
SecondEssays []Essay `gorm:"foreignKey:SecondAuthorID;references:;"`
}
// CREATE TABLE `msb_essay` (
// `id` bigint unsigned NOT NULL AUTO_INCREMENT,
// `first_author_id` bigint unsigned DEFAULT NULL,
// `second_author_id` bigint unsigned DEFAULT NULL,
// `author_id` bigint unsigned DEFAULT NULL,
// PRIMARY KEY (`id`),
// KEY `idx_msb_essay_deleted_at` (`deleted_at`),
// KEY `fk_msb_author_essays` (`author_id`),
// KEY `fk_msb_author_first_essays` (`first_author_id`),
// KEY `fk_msb_author_second_essays` (`second_author_id`),
// CONSTRAINT `fk_msb_author_essays` FOREIGN KEY (`author_id`) REFERENCES `msb_author` (`id`),
// CONSTRAINT `fk_msb_author_first_essays` FOREIGN KEY (`first_author_id`) REFERENCES `msb_author` (`id`),
// CONSTRAINT `fk_msb_author_second_essays` FOREIGN KEY (`second_author_id`) REFERENCES `msb_author` (`id`)
//) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
// 论文内容
type Essay struct {
gorm.Model
Subject string
Content string
// 自定义关联字段
FirstAuthorID *uint
SecondAuthorID *uint
FirstAuthor Author `gorm:"foreignKey:FirstAuthorID;references:;"`
SecondAuthor Author `gorm:"foreignKey:SecondAuthorID;references:;"`
// 外键字段
AuthorID *uint
// 属于某个作者
// belongs to
Author Author
// 拥有一个论文元信息
// has one
EssayMate EssayMate
// 拥有多个Tag
// many to many
Tags []Tag `gorm:"many2many:essay_tag"`
}
// 论文元信息
type EssayMate struct {
gorm.Model
Keyword string
Description string
// 外键字段
EssayID *uint
// 属于一个论文内容,比较少用
//Essay *Essay
}
type Tag struct {
gorm.Model
Title string
// 拥有多个Essay
// many to many
Essays []Essay `gorm:"many2many:essay_tag"`
}