gorm操练记录
这个数据库的定义蛮全的,先作个记录。
package main
import (
"time"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
type User struct {
gorm.Model
Birthday time.Time
Age int
Name string `gorm:"size:255"` // string默认长度为255, 使用这种tag重设。
Num int `gorm:"AUTO_INCREMENT"` // 自增
CreditCard CreditCard // One-To-One (拥有一个 - CreditCard表的UserID作外键)
Emails []Email // One-To-Many (拥有多个 - Email表的UserID作外键)
BillingAddress Address // One-To-One (属于 - 本表的BillingAddressID作外键)
//BillingAddressID sql.NullInt64
ShippingAddress Address // One-To-One (属于 - 本表的ShippingAddressID作外键)
ShippingAddressID int
IgnoreMe int `gorm:"-"` // 忽略这个字段
Languages []Language `gorm:"many2many:user_languages;"` // Many-To-Many , 'user_languages'是连接表
}
type Email struct {
ID int
UserID int `gorm:"index"` // 外键 (属于), tag `index`是为该列创建索引
Email string `gorm:"type:varchar(100);unique_index"` // `type`设置sql类型, `unique_index` 为该列设置唯一索引
Subscribed bool
}
type Address struct {
ID int
Address1 string `gorm:"not null;unique"` // 设置字段为非空并唯一
Address2 string `gorm:"type:varchar(100);unique"`
//Post sql.NullString `gorm:"not null"`
}
type Language struct {
ID int
Name string `gorm:"index:idx_name_code"` // 创建索引并命名,如果找到其他相同名称的索引则创建组合索引
Code string `gorm:"index:idx_name_code"` // `unique_index` also works
}
type CreditCard struct {
gorm.Model
UserID uint
Number string
}
func main() {
db, err := gorm.Open("mysql", "root:password@(127.0.0.1:3306)/gorm?charset=utf8&parseTime=True&loc=Local")
if err != nil {
panic(err)
}
db.DB().SetMaxIdleConns(10)
db.DB().SetMaxOpenConns(100)
db.SingularTable(true)
defer db.Close()
// 自动迁移模式
db.AutoMigrate(&User{}, &Email{}, &Address{}, &Language{}, &CreditCard{})
/*
db.Create(&Product{Code: "L1212", Price: 1000})
// 读取
var product Product
db.First(&product, 1) // 查询id为1的product
db.First(&product, "code = ?", "L1212") // 查询code为l1212的product
// 更新 - 更新product的price为2000
db.Model(&product).Update("Price", 2000)
// 删除 - 删除product
db.Delete(&product)
tx := db.Begin()
r1 := db.DropTable(&Address{})
r2 := db.DropTableIfExists(&CreditCard{})
if r1.Error != nil || r2.Error != nil {
tx.Rollback()
return
}
tx.Commit()
*/
}
gorm操练记录的更多相关文章
- gorm创建记录
1. 简单创建记录 user := User{Name: "李四", Age: 88, Birthday: time.Now()} ret := db.Create(&u ...
- gorm创建记录及设置字段默认值
package main import ( "database/sql" "gorm.io/driver/mysql" "gorm.io/gorm&q ...
- gorm 更新数据时,0值会被忽略
原文: https://www.tizi365.com/archives/22.html ------------------------------------------------------- ...
- PhalGo-介绍
PhalGo-介绍 phalgo是一个Go语言的一体化开发框架,主要用于API开发应为使用ECHO框架作为http服务web程序一样可以使用,牛顿曾经说过"如果我比别人看得远,那是因为我站在 ...
- Grails 对象关联映射 (GORM) 一
转自:http://justjavac.iteye.com/blog/701445 Domain 类是任何商业应用的核心. 他们保存事务处理的状态,也处理预期的行为. 他们通过关联联系在一起, one ...
- gorm的日志模块源码解析
gorm的日志模块源码解析 如何让gorm的日志按照我的格式进行输出 这个问题是<如何为gorm日志加traceId>之后,一个群里的朋友问我的.如何让gorm的sql日志不打印到控制台, ...
- Go项目中beego的orm使用和gorm的使用
按照beego官方文档练习ORM的使用,model创建完始终没找到办法创建表,于是使用gorm翻译文档和官方文档进行了练习,使用起来还是比较简单. 安装: 方法一:Terminal打开,go get ...
- 【EF6学习笔记】(二)操练 CRUD 增删改查
本篇原文链接: Implementing Basic CRUD Functionality 说明:学习笔记参考原文中的流程,为了增加实际操作性,并能够深入理解,部分地方根据实际情况做了一些调整:并且根 ...
- Gorm使用详解
1.什么是Gorm go语言编写的orm框架 特点: 1)全功能ORM 2)关联(包含一个,包含多个,属于,多对多) 3)Callbacks(创建/保存/更新/删除/查找前后回调) 4)预加载 5)事 ...
随机推荐
- Spring Boot 自动装配(二)
目录 目录 前言 1.起源 2.Spring Boot 自动装配实现 2.1.@EnableAutoConfiguration 实现 2.1.1. 获取默认包扫描路径 2.1.2.获取自动装配的组件 ...
- LNMP的搭建 及地址转换
1. LNMP 先安装nginx yum -y install gcc openssl-devel pcre-devel wget http://nginx.org/download/ngin ...
- 解决 Docker Hadoop ssh "Connection to * closed".问题
Docker 最近很火, 可以快速轻量级地虚拟出多个node,所以打算在Docker中跑Hadoop伪分布式应用. 其实要做出个简单的版本倒是不难,主要在 建立ssh无密码登录本机时,出现刚登录上去, ...
- Java修炼——ArrayList常用的方法以及三种方式遍历集合元素。
List接口ArrayList用法详解 ArrayList常用方法: 1. List.add():添加的方法(可以添加字符串,常量,以及对象) List list=new ArrayList(); l ...
- 回文自动机pam
目的:类似回文Trie树+ac自动机,可以用来统计一些其他的回文串相关的量 复杂度:O(nlogn) https://blog.csdn.net/Lolierl/article/details/999 ...
- Binary Search Tree analog
Description Binary Search Tree, abbreviated as BST, is a kind of binary tree maintains the following ...
- 从源码看Nacos的设计
目录 客户端与集群的交互 数据同步 实例信息同步 服务集群信息 关于priv-raft协议 Nacos集群在k8s中的实践 这片博文来源于我在公司部门内的分享,我隐去了和公司项目相关的部分,重新整理, ...
- 【JS】285- 拆解 JavaScript 中的异步模式
JavaScript 中有很多种异步编程的方式.callback.promise.generator.async await 甚至 RxJS.我最初接触不同的异步模式时,曾想当然的觉得 promise ...
- JS基础-全局内置对象
对象 JS中有那些内置对象 数据封装类对象 String.Array.Object.Boolean.Number 其他对象 Math.Date.RegExp.Error.Function.Argume ...
- Linux 怎么清理缓存
linux清理缓存的命令 查看缓存的命令 free -m 清理缓存的命令 echo 1 > /proc/sys/vm/drop_caches echo 2 > /proc/sys/v ...