当前位置:网站首页>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
边栏推荐
- Live server usage
- Right click the idea file to create new. There is no solution to create new servlet
- Restricted linear table
- Design and implementation of spark offline development framework
- SAP HR reward and punishment information export
- MySQL Architecture
- Arbre binaire équilibré [Arbre AVL] - Insérer et supprimer
- Installing gradle
- Take you hand in hand to build Eureka client with idea
- Summary of SQL single table query 2020.7.27
猜你喜欢
Chisel tutorial - 02 Chisel environment configuration and implementation and testing of the first chisel module
Idea automatically generates serialVersionUID
Chisel tutorial - 03 Combinatorial logic in chisel (chisel3 cheat sheet is attached at the end)
C inheritance and interface design polymorphism
Live server usage
B_ QuRT_ User_ Guide(38)
Take you hand in hand to build Eureka client with idea
SAP HR 家庭成员信息
Benchmarking Detection Transfer Learning with Vision Transformers(2021-11)
Open source hardware small project: anxinco esp-c3f control ws2812
随机推荐
Where are you going
MySQL Architecture
SAP HR labor contract information 0016
关于CH32库函数与STM32库函数的区别
postgis学习
C language greedy snake
Mobile heterogeneous computing technology - GPU OpenCL programming (basic)
SQL 使用in关键字查询多个字段
2022.7.7-----leetcode.648
B_ QuRT_ User_ Guide(39)
SAP HR social work experience 0023
Dependency injection
Progress broadcast | all 29 shield machines of Guangzhou Metro Line 7 have been launched
Get started with mongodb
codeforces每日5题(均1500)-第八天
Boost regex library source code compilation
Pycharm basic settings latest version 2022
Design and implementation of spark offline development framework
C simple question 2
【路径规划】使用垂距限值法与贝塞尔优化A星路径