当前位置:网站首页>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
边栏推荐
猜你喜欢

ESP at installation esp8266 and esp32 versions

Flash download setup

Get started with mongodb

MongoDB快速入门
![[stm32+esp8266 connects to Tencent cloud IOT development platform 3] stm32+esp8266-01s dynamically registers devices on Tencent cloud (at instruction mode) -- with source code](/img/55/ab50ead2564498cb214d98ac5b9c3d.jpg)
[stm32+esp8266 connects to Tencent cloud IOT development platform 3] stm32+esp8266-01s dynamically registers devices on Tencent cloud (at instruction mode) -- with source code

0-1 knapsack problem
![[STM32 + esp-12s connect Tencent cloud IOT development platform 1] creation of cloud platform and burning of at firmware](/img/bc/8241a339cca9b7af475169dba39c10.jpg)
[STM32 + esp-12s connect Tencent cloud IOT development platform 1] creation of cloud platform and burning of at firmware

蓝桥ROS中使用fishros一键安装

Anxinco esp32-a1s development board is adapted to Baidu dueros routine to realize online voice function

Extended tree (I) - graphic analysis and C language implementation
随机推荐
Chisel tutorial - 02 Chisel environment configuration and implementation and testing of the first chisel module
二叉排序树【BST】——创建、查找、删除、输出
Pycharm essential plug-in, change the background (self use, continuous update) | CSDN creation punch in
Apng2gif solutions to various problems
Anxin vb01 offline voice module access intelligent curtain guidance
Anxinco EC series modules are connected to the multi protocol access products of onenet Internet of things open platform
May day d-light
2022 certified surveyors are still at a loss when preparing for the exam? Teach you how to take the exam hand in hand?
C - linear table
受限线性表
May day C - most
Chisel tutorial - 00 Ex.scala metals plug-in (vs Code), SBT and coursier exchange endogenous
0-1 knapsack problem
codeforces每日5题(均1500)-第八天
How to change the formula picture in the paper directly into the formula in word
Take you hand in hand to build Eureka client with idea
保证接口数据安全的10种方案
平衡二叉树【AVL树】——插入、删除
Benchmarking Detection Transfer Learning with Vision Transformers(2021-11)
Interface