在 Gorm 中使用模型连接两个表的最佳方法是什么?
What is the best way to join two tables using models in Gorm?
我无法使用 GORM 连接两个 table。经过所有研究,我发现预加载可以解决问题。但是,我不清楚如何使用它。我尝试了很多方法,但 none 奏效了。我有两个 table。用户和角色。每个用户将关联一个角色。
这是用户 table
的架构
CREATE TABLE `users` (
`id` int NOT NULL AUTO_INCREMENT,
`first_name` varchar(45) NOT NULL,
`last_name` varchar(45) NOT NULL,
`email` varchar(45) NOT NULL,
`password` varchar(45) NOT NULL,
`app_id` int NOT NULL,
`created_at` datetime DEFAULT NULL,
`deleted_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
`verifications` json DEFAULT NULL COMMENT 'A json Object in a specific format.\n{\n Verification_Code : {\n isCompleted: Bool,\n message: "Some custom message",\n extraInfo: {}\n }\n}',
`role_id` int NOT NULL,
`status` int NOT NULL COMMENT '0 = Good Status and active account\n> 0 = Bad Status and deactive account',
`company_id` int DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_index` (`email`,`app_id`),
KEY `role_key_colmn_idx` (`role_id`),
KEY `app_key_column_idx` (`app_id`),
KEY `company_id` (`company_id`),
CONSTRAINT `app_key_column` FOREIGN KEY (`app_id`) REFERENCES `applications` (`id`),
CONSTRAINT `role_key_colmn` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`),
CONSTRAINT `users_ibfk_1` FOREIGN KEY (`company_id`) REFERENCES `companies` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
角色架构Table:
CREATE TABLE `roles` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`description` varchar(45) DEFAULT NULL,
`app_id` int NOT NULL,
`code` varchar(45) NOT NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
`deleted_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `code_UNIQUE` (`code`),
KEY `App_Id_conn_idx` (`app_id`),
CONSTRAINT `App_Id_conn` FOREIGN KEY (`app_id`) REFERENCES `applications` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
用户实体模型:
type User struct {
gorm.Model
FirstName string `gorm:"column:first_name;not_null"`
LastName string `gorm:"column:last_name;not_null"`
Email string `gorm:"column:email;not_null"`
Password string `gorm:"column:password;not_null"`
AppId uint `gorm:"column:app_id;not_null"`
Verifications string `gorm:"column:verifications;not_null"`
Status int `gorm:"column:status;not_null"`
Role Role
Company Company
}
func (User) TableName() string { return "users" }
角色实体模型:
type Role struct {
gorm.Model
Name string `gorm:"column:name;not_null"`
Description string `gorm:"column:description"`
AppId uint `gorm:"column:app_id;not_null"`
Code string `gorm:"column:code;not_null;"`
}
func (Role) TableName() string { return "roles" }
这是我正在使用的查询:
err := r.db.Where(&Entity.User{AppId: appId, Email: username, Password: password}).Take(&u).Error;
这是填充用户信息而不是角色信息。任何帮助将不胜感激。在 spring 引导中,它过去常常通过设置一些 @annotations 来自动填充,但在这里我不确定它是如何工作的。
首先,您需要在模型本身中添加外键字段,并在字段标记中指定列名称,除非您对外键列使用 default GORM 命名约定。然后需要在引用模型上为外键添加gorm字段标签gorm:"foreignkey:RoleId"
type User struct {
gorm.Model
FirstName string `gorm:"column:first_name;not_null"`
LastName string `gorm:"column:last_name;not_null"`
Email string `gorm:"column:email;not_null"`
Password string `gorm:"column:password;not_null"`
AppId uint `gorm:"column:app_id;not_null"`
Verifications string `gorm:"column:verifications;not_null"`
Status int `gorm:"column:status;not_null"`
Role Role `gorm:"foreignkey:RoleId"`
RoleId int
Company Company
}
然后要预先加载您可以使用的角色 Preload
err := r.db.Preload('Role').Where(&Entity.User{AppId: appId, Email: username, Password: password}).Take(&u).Error;
我无法使用 GORM 连接两个 table。经过所有研究,我发现预加载可以解决问题。但是,我不清楚如何使用它。我尝试了很多方法,但 none 奏效了。我有两个 table。用户和角色。每个用户将关联一个角色。
这是用户 table
的架构CREATE TABLE `users` (
`id` int NOT NULL AUTO_INCREMENT,
`first_name` varchar(45) NOT NULL,
`last_name` varchar(45) NOT NULL,
`email` varchar(45) NOT NULL,
`password` varchar(45) NOT NULL,
`app_id` int NOT NULL,
`created_at` datetime DEFAULT NULL,
`deleted_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
`verifications` json DEFAULT NULL COMMENT 'A json Object in a specific format.\n{\n Verification_Code : {\n isCompleted: Bool,\n message: "Some custom message",\n extraInfo: {}\n }\n}',
`role_id` int NOT NULL,
`status` int NOT NULL COMMENT '0 = Good Status and active account\n> 0 = Bad Status and deactive account',
`company_id` int DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_index` (`email`,`app_id`),
KEY `role_key_colmn_idx` (`role_id`),
KEY `app_key_column_idx` (`app_id`),
KEY `company_id` (`company_id`),
CONSTRAINT `app_key_column` FOREIGN KEY (`app_id`) REFERENCES `applications` (`id`),
CONSTRAINT `role_key_colmn` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`),
CONSTRAINT `users_ibfk_1` FOREIGN KEY (`company_id`) REFERENCES `companies` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
角色架构Table:
CREATE TABLE `roles` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`description` varchar(45) DEFAULT NULL,
`app_id` int NOT NULL,
`code` varchar(45) NOT NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
`deleted_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `code_UNIQUE` (`code`),
KEY `App_Id_conn_idx` (`app_id`),
CONSTRAINT `App_Id_conn` FOREIGN KEY (`app_id`) REFERENCES `applications` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
用户实体模型:
type User struct {
gorm.Model
FirstName string `gorm:"column:first_name;not_null"`
LastName string `gorm:"column:last_name;not_null"`
Email string `gorm:"column:email;not_null"`
Password string `gorm:"column:password;not_null"`
AppId uint `gorm:"column:app_id;not_null"`
Verifications string `gorm:"column:verifications;not_null"`
Status int `gorm:"column:status;not_null"`
Role Role
Company Company
}
func (User) TableName() string { return "users" }
角色实体模型:
type Role struct {
gorm.Model
Name string `gorm:"column:name;not_null"`
Description string `gorm:"column:description"`
AppId uint `gorm:"column:app_id;not_null"`
Code string `gorm:"column:code;not_null;"`
}
func (Role) TableName() string { return "roles" }
这是我正在使用的查询:
err := r.db.Where(&Entity.User{AppId: appId, Email: username, Password: password}).Take(&u).Error;
这是填充用户信息而不是角色信息。任何帮助将不胜感激。在 spring 引导中,它过去常常通过设置一些 @annotations 来自动填充,但在这里我不确定它是如何工作的。
首先,您需要在模型本身中添加外键字段,并在字段标记中指定列名称,除非您对外键列使用 default GORM 命名约定。然后需要在引用模型上为外键添加gorm字段标签gorm:"foreignkey:RoleId"
type User struct {
gorm.Model
FirstName string `gorm:"column:first_name;not_null"`
LastName string `gorm:"column:last_name;not_null"`
Email string `gorm:"column:email;not_null"`
Password string `gorm:"column:password;not_null"`
AppId uint `gorm:"column:app_id;not_null"`
Verifications string `gorm:"column:verifications;not_null"`
Status int `gorm:"column:status;not_null"`
Role Role `gorm:"foreignkey:RoleId"`
RoleId int
Company Company
}
然后要预先加载您可以使用的角色 Preload
err := r.db.Preload('Role').Where(&Entity.User{AppId: appId, Email: username, Password: password}).Take(&u).Error;