当前位置:网站首页>Gorm Association summary
Gorm Association summary
2022-07-07 23:44:00 【Cough, Hello, please give me more advice!】
Database design
surface 1:
CREATE TABLE `blog_user` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(50) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',
`password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',
`created_at` datetime(3) DEFAULT NULL,
`updated_at` datetime(3) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_blog_user_created_at` (`created_at`),
KEY `idx_blog_user_updated_at` (`updated_at`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
--------------------------------------------------------------------------------------------------------------------
surface 2:
CREATE TABLE `blog_article` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',
`user_id` bigint unsigned NOT NULL,
`is_hot` tinyint NOT NULL,
`created_at` datetime(3) DEFAULT NULL,
`updated_at` datetime(3) DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
1.Belongs To Belong to
The relationship between users and articles The article table exists in the user table id As Foreign keys , article Belong to user
1-1 User Of Model Structure
type User struct {
models.BaseModel
UserName string `gorm:"column:username;" json:"username"`
Password string `json:"-"`
models.CommonTimestampsField
}
1-2 Article Of Model Structure
type Article struct {
models.BaseModel
Title string `json:"title"`
IsHot int8 `json:"is_hot"`
UserId int64 `json:"user_id"` // Foreign keys
User users.User `json:"user" gorm:"ForeignKey:UserId"`// Associated object
}
Inquire about :
Joins or Preload Preloading
var results []Article
database.DB.Model(&Article{}).Joins("User").Find(&results).Error, results
2. hasOne one-on-one
The relationship between users and articles The article table exists in the user table id As Foreign keys , user And article one-on-one ( Assume One can only have one piece of article )
2-1 User Of Model Structure
type User struct {
models.BaseModel
UserName string `gorm:"column:username;" json:"username"`
Password string `json:"-"`
Article article.Article `gorm:"foreignKey:UserId"`// One to one connection
models.CommonTimestampsField
}
2-2 Article Of Model Structure
type Article struct {
models.BaseModel
Title string `json:"title"`
IsHot int8 `json:"is_hot"`
UserId int64 `json:"user_id"` // Foreign keys
}
Inquire about :
Preload Preloading
var results []Article
database.DB.Model(&User{}).Preload("Article").Find(&results).Error, results
3.Has Many One to many The relationship between users and articles The article table exists in the user table id As Foreign keys , user And article One to many
3-1 User Of Model Structure
type User struct {
models.BaseModel
UserName string `gorm:"column:username;" json:"username"`
Password string `json:"-"`
Article []article.Article `gorm:"foreignKey:UserId"`
models.CommonTimestampsField
}
3-2 Article Of Model Structure
type Article struct {
models.BaseModel
Title string `json:"title"`
IsHot int8 `json:"is_hot"`
UserId int64 `json:"user_id"` // Foreign keys
}
Inquire about :
Preload Preloading
var results []Article
database.DB.Model(&User{}).Preload("Article").Find(&results).Error, results
4.Many To Many
4-1. newly added It's the body surface language & blog_user_has_language
CREATE TABLE `blog_language` (
`id` int NOT NULL AUTO_INCREMENT,
`title` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
------------------------------------------------------------------------------------
CREATE TABLE `blog_user_has_language` (
`user_id` int NOT NULL DEFAULT '0',
`language_id` int NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
4-2. Structure
type User struct {
models.BaseModel
UserName string `gorm:"column:username;" json:"username"`
Password string `json:"-"`
Languages []language.Language `gorm:"many2many:user_has_language;"`
models.CommonTimestampsField
}
4-3. Inquire about :
Preload Preloading
var results []Article
database.DB.Model(&User{}).Preload("Languages").Find(&results).Error, results
边栏推荐
- One of the anti climbing methods
- Alibaba cloud MySQL cannot connect
- Open source hardware small project: anxinco esp-c3f control ws2812
- AITM3.0005 烟雾毒性测试
- 2022.7.7-----leetcode.648
- Arbre binaire équilibré [Arbre AVL] - Insérer et supprimer
- List. How to achieve ascending and descending sort() 2020.8.6
- Anti climbing means cracking the second
- Fibonacci number of dynamic programming
- Understand TCP's three handshakes and four waves with love
猜你喜欢

Dataguard 主备清理归档设置

Live server usage

Progress broadcast | all 29 shield machines of Guangzhou Metro Line 7 have been launched
![P1067 [noip2009 popularity group] polynomial output (difficult, pit)](/img/1f/a798879a0d65eccefa339b288f2102.jpg)
P1067 [noip2009 popularity group] polynomial output (difficult, pit)

2022 certified surveyors are still at a loss when preparing for the exam? Teach you how to take the exam hand in hand?

0-1背包问题

Understand TCP's three handshakes and four waves with love

Chisel tutorial - 03 Combinatorial logic in chisel (chisel3 cheat sheet is attached at the end)

Pycharm basic settings latest version 2022

SAP HR labor contract information 0016
随机推荐
Understand TCP's three handshakes and four waves with love
【LeetCode】20、有效的括号
SLAM面试总结
Jisuan Ke - t3104
C number of words, plus ¥, longest word, average value
AITM3.0005 烟雾毒性测试
C simple question 2
ASP. Net open web page
Live server usage
Come on, brother
解析token的网址
Progress broadcast | all 29 shield machines of Guangzhou Metro Line 7 have been launched
@Configuration注解的详细介绍
SAP HR labor contract information 0016
aws-aws help报错
ASP. Net core middleware request processing pipeline
Take you hand in hand to build feign with idea
平衡二叉树【AVL树】——插入、删除
An example analysis of MP4 file format parsing
StringUtils工具类