• 【GoWeb项目-个人Blog】数据库表设计


    项目地址:https://gitee.com/illlloooovvvvcode/daily-blog
    主要任务:数据库表设计

    数据库表设计

    1. ER图

    目前,主体上只有四个模块,设计五张表

    • posts 文章表
    • tags 标签表
    • post_tags 文章-标签表
    • categorys 分类表
    • comments 评论表
      在这里插入图片描述

    2.创建model

    位置:/daily-blog/model

    2.1 post

    
    type Post struct {
    	gorm.Model
    	PostId       string `gorm:"post_id;type:CHAR(32) NOT NULL;comment:post唯一id"` // uuid生成
    	Title        string `gorm:"column:title; type:VARCHAR(255) NOT NULL;comment:标题"`
    	Desc         string `gorm:"column:desc;type:VARCHAR(1024) NOT NULL; comment:描述"`
    	Content      string `gorm:"column:content;type:TEXT NOT NULL; comment:内容"`
    	Author       string `gorm:"column:author;type:VARCHAR(32)  NOT NULL;comment:作者"`
    	CategoryId   int64  `gorm:"column:category_id;type:CHAR(32) NOT NULL; comment:类别唯一id"`
    	CategoryName string `gorm:"column:category; type:VARCHAR(32)  NOT NULL;comment:分类名"`
    	Love         int    `gorm:"column:love; comment:点赞次数"`
    	Watch        int    `gorm:"column:watch; comment:浏览次数"`
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    2.2 tag

    type Tag struct {
    	gorm.Model
    	TagId   string `gorm:"column:tag_id;type:CHAR(32)  NOT NULL; comment:tag唯一id"`
    	TagName string `gorm:"column:tag_name;type:VARCHAR(32)  NOT NULL; comment:标签名"`
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.3 post_tags

    type PostTag struct {
    	gorm.Model
    	PostId  string `gorm:"column:post_id;type:CHAR(32)  NOT NULL; comment:post唯一id"`
    	TagId   string `gorm:"column:tag_id;type:CHAR(32)  NOT NULL; comment:tag唯一id"`
    	TagName string `gorm:"column:tag_name;type:VARCHAR(32)  NOT NULL;comment:标签名"`
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2.4 comment

    type Comment struct {
    	gorm.Model
    	CommentId string `gorm:"column:comment_id;type:CHAR(32)  NOT NULL; comment:评论id"`
    	Author    string `gorm:"column:author;type:VARCHAR(32)  NOT NULL;  comment:评论人"`
    	Content   string `gorm:"column:content;type:VARCHAR(1024) NOT NULL;  comment:评论内容"`
    	PostId    string `gorm:"column:post_id;type:CHAR(32)  NOT NULL;  comment:评论文章的id"`
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2.5 category

    type Category struct {
    	gorm.Model
    	CategoryId string `gorm:"column:category_id;type:CHAR(32)  NOT NULL;comment:category唯一id"`
    	Name       string `gorm:"column:name;type:VARCHAR(32)  NOT NULL;comment:分类名"`
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.6 指定表名

    每一个model可以指定TableName·方法指定自动迁移时的表名
    例如:

    func (PostTag) TableName() string {
    	return "post_tags"
    }
    
    • 1
    • 2
    • 3

    3. 迁移表结构

    /daily-blog/global/init-db.go 中

    func initTables(db *gorm.DB) {
    	db.AutoMigrate(&model.Post{})
    	db.AutoMigrate(&model.Tag{})
    	db.AutoMigrate(&model.PostTag{})
    	db.AutoMigrate(&model.Category{})
    	db.AutoMigrate(&model.Comment{})
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    4. 查看表结构

    在这里插入图片描述

  • 相关阅读:
    [2023.09.26]: JsValue的转换体验与as关键字的浅析
    Neutron — DHCP Agent 实现原理
    Titanic 泰坦尼克号预测-Tensorflow 方法-【Kaggle 比赛】
    Airtext连接chrome谷歌浏览器报错
    GBase 8c 创建和管理表(一)
    分布式:Docker
    LabVIEW在 XY Graph中选择一组点
    R语言计算方差分析的F值和P值
    IDEA在多线程环境下断点调试-验证synchronized监视锁的运行状态
    淘宝获取收货地址列表的 API
  • 原文地址:https://blog.csdn.net/weixin_45919793/article/details/126709284