golang Gorm与数据库完整性约束详解

数据库约束要点:

主键约束(非空且唯一)外键约束 子表外键字段的值必须在主表被参照字段值得范围内,或者为NULL;外键参照的必须是主表的主键或唯一键;主表主键/唯一键被子表参照时,主表相应记录不允许被删除

在golang中,采用orm对数据库进行建模是比较方便的。grom是其中一个比较流行的orm工具。

本篇基于golang、grom1.91、和PostgreSQL来进行说明。

注:本文的例子是极端情况,一般情况只是单字段主键。

1、实体完整性:

每个关系(表)有且仅有一个主键,每一个主键值必须唯一,而且不允许为“空”(NULL)或重复。

type Product struct {
Code string `gorm:"primary_key"`
Price uint
UserID uint //`sql:"type:bigint REFERENCES users(id) on update no action on delete cascade"`
UserCode string //`sql:"type:bigint REFERENCES users(code) on update no action on delete cascade"`
User User //`gorm:"foreignkey:UserID;association_foreignkey:ID"`
gorm.Model
}

在利用gorm的db对象创建表,其使用的SQL如下:

CREATE TABLE public.products
(
 code text COLLATE pg_catalog."default" NOT NULL,
 price integer,
 user_id integer,
 user_code text COLLATE pg_catalog."default",
 id integer NOT NULL DEFAULT nextval('products_id_seq'::regclass),
 created_at timestamp with time zone,
 updated_at timestamp with time zone,
 deleted_at timestamp with time zone,
 CONSTRAINT products_pkey PRIMARY KEY (code, id)
)
WITH (
 OIDS = FALSE
)
TABLESPACE pg_default;
ALTER TABLE public.products
 OWNER to postgres; 

-- Index: idx_products_deleted_at 

-- DROP INDEX public.idx_products_deleted_at; 

CREATE INDEX idx_products_deleted_at
 ON public.products USING btree
 (deleted_at)
 TABLESPACE pg_default;

说明:

1.1、grom.Model是gorm预定义的结构,用于实现软删除,定义如下:

type Model struct {
 ID uint `gorm:"primary_key"`
 CreatedAt time.Time
 UpdatedAt time.Time
 DeletedAt *time.Time `sql:"index"`
}

它里面已经定义了主键,在本例中,我们还定义了一个主键:

Code string `gorm:"primary_key"`

从SQL输出我们看到:

CONSTRAINT products_pkey PRIMARY KEY (code, id)

因此,Gorm实现了完全的实体完整性支持,即可以支持字段主键,也可以支持联合主键。

1.2、对比结构体和sql语句可以看出

1.2.1 表名=结构体名小写的复数 例子:Product变为 products

1.2.2 字段名=结构体成员名大写分隔的子串小写形式用下划线连接 例子:ID变为id CreatedAt变为created_at

1.3、前述1.1和1.2构成了Gorm的convention,它的文档里有,默认情况下,就是这么处理,但是用户可以不用gorm.Model,自定义表名、字段名,都可以支持。

2、域完整性:

是指数据库表中的列必须满足某种特定的数据类型或约束,又叫用户定义完整性。包括:字段类型、值域、小数位数、CHECK、FOREIGN KEY 约束和DEFAULT、 NOT NULL。它们有的定义在字段上,有的定义在表上。例如:FOREIGN KEY 约束在PostgresSQL中,就是在表级别定义的;而,字段类型、长度、小数位数就是在字段上定义的。

2.1 通过结构体tag

`gorm:"xxx"` ,在字段上可以使用:type、size、precision、not null、default,Gorm就可以完成这些域完整性的定义

2.2 FOREIGN KEY 约束

2.2.1 单字段外键约束

type Product struct {
 Code string //`gorm:"primary_key"`
 Price uint
 UserID uint //`sql:"type:bigint REFERENCES users(id) on update no action on delete cascade"`
  //UserCode string //`sql:"type:bigint REFERENCES users(code) on update no action on delete cascade"`
 User User //`gorm:"foreignkey:UserID;association_foreignkey:ID"`
 gorm.Model
}
type User struct {
  //Code string `gorm:"primary_key"`
 Name string
 gorm.Model
  //Product Product //`gorm:"EMBEDDED"`
}

上面的代码按照gorm文档,创建了一个products belongs to user关系。执行的sql是:

CREATE TABLE "users" ("code" text,"name" text,"id" serial,"created_at" timestamp with time zone,"updated_at" timestamp with time zone,"deleted_at" timestamp with time zone , PRIMARY KEY ("code","id"))
CREATE TABLE "users" ("code" text,"name" text,"id" serial,"created_at" timestamp with time zone,"updated_at" timestamp with time zone,"deleted_at" timestamp with time zone , PRIMARY KEY ("code","id"))

我们看到,gorm没有添加任何约束。按照Gorm文档,这就是belongs to标准定义。

它不添加外键约束。

那么,改为显式标准的形式,采用foreignkey tag呢?

type Product struct {
 Code string //`gorm:"primary_key"`
 Price uint
 UserID uint //`sql:"type:integer REFERENCES users(id)`// on update no action on delete cascade"`
  //UserCode string //`sql:"type:bigint REFERENCES users(code) on update no action on delete cascade"`
  //UserID uint
 User User `gorm:"foreignkey:UserID;association_foreignkey:ID"` //`gorm:"foreignkey:UserID;association_foreignkey:ID"`
 gorm.Model
}
type User struct {
  //Code string `gorm:"primary_key"`
 Name string
 gorm.Model
  //Product Product //`gorm:"EMBEDDED"`
}

执行的sql是:

CREATE TABLE "users" ("name" text,"id" serial,"created_at" timestamp with time zone,"updated_at" timestamp with time zone,"deleted_at" timestamp with time zone , PRIMARY KEY ("id"))
CREATE TABLE "products" ("code" text,"price" integer,"user_id" integer,"id" serial,"created_at" timestamp with time zone,"updated_at" timestamp with time zone,"deleted_at" timestamp with time zone , PRIMARY KEY ("id"))

也没有添加任何外键约束。

因此,gorm tag 的 foreignkey 和 association_foreignkey并不会添加外键约束。

但是,我们可以用sql tag来添加外键约束!!!如下:

type Product struct {
 Code string //`gorm:"primary_key"`
 Price uint
 UserID uint `sql:"type:integer REFERENCES users(id) on update no action on delete no action"`
  //UserCode string //`sql:"type:bigint REFERENCES users(code) on update no action on delete cascade"`
  //UserID uint
 User User `gorm:"foreignkey:UserID;association_foreignkey:ID"` //`gorm:"foreignkey:UserID;association_foreignkey:ID"`
 gorm.Model
}
type User struct {
  //Code string `gorm:"primary_key"`
 Name string
 gorm.Model
  //Product Product //`gorm:"EMBEDDED"`
}

创建products表的语句:

 CREATE TABLE "products" ("code" text,"price" integer,"user_id" integer REFERENCES users(id) on update no action on delete no action,"id" serial,"created_at" timestamp with time zone,"updated_at" timestamp with time zone,"deleted_at" timestamp with time zone , PRIMARY KEY ("id"))

注意,当使用sql tag时,不像gorm tag,它要你用数据库表名和字段名,而gorm就只需要你使用结构体和其成员名即可。

外键被定义了,此时,可以满足外键约束,如前述,具体是:

子表外键字段的值必须在主表被参照字段值得范围内,或者为NULL;外键参照的必须是主表的主键或唯一键;主表主键/唯一键被子表参照时,主表相应记录不允许被删除

此时外键约束的名字是数据库自己取的,可能长了,你可以自定义:

UserID uint `sql:"type:integer constraint ref REFERENCES users(id) on update no action on delete no action"`

加上 constraint xxx,就可以为约束取名为xx了。

上述外键约束是在定义结构体时,在结构体成员上定义的,因此翻译为sql语句就变成了对字段的外键约束,那如果要定义参照联合主键之类的外键呢?就不能在结构体中定义,而要使用gorm的api了。

2.2.2 多字段外键约束

type Product struct {
 Code string //`gorm:"primary_key"`
 Price uint
  //UserID uint `sql:"type:integer REFERENCES users(id) on update no action on delete no action"`
  //UserCode string //`sql:"type:bigint REFERENCES users(code) on update no action on delete cascade"`
 UserCode string
 UserID uint
 User User //`gorm:"foreignkey:UserID;association_foreignkey:ID"`//`gorm:"foreignkey:UserID;association_foreignkey:ID"`
 gorm.Model
}
type User struct {
 Code string `gorm:"primary_key"`
 Name string
 gorm.Model
  //Product Product //`gorm:"EMBEDDED"`
}

在程序中使用:

postgres. Model( &Product{}). AddForeignKey( "user_id,user_code", "users(id,code)", "no action", "no action")

这样,products表就有约束:

CONSTRAINT products_user_id_user_code_users_id_code_foreign FOREIGN KEY (user_code, user_id)
REFERENCES public.users (code, id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION

如此就OK了。这里约束的名字就很长了,api没有给你自己取名字的机会。

2.3 check约束

type Product struct {
 Code string //`gorm:"primary_key"`
 Price uint
 UserID uint `sql:"type:integer check(code!='')"`
 UserCode string //`sql:"type:bigint constraint ref REFERENCES users(code) on update no action on delete cascade"`
  //UserCode string
  //UserID uint
 User User //`gorm:"foreignkey:UserID;association_foreignkey:ID"`//`gorm:"foreignkey:UserID;association_foreignkey:ID"`
 gorm.Model
}

这样就行。看起来这个check和userID没有什么关系,是的,check会被定义到表上:

ALTER TABLE public.products

ADD CONSTRAINT products CHECK (code <> ''::text);

因此,Check也完美了,找个结构体的字段,然后加上check就行了。

3、参照完整性:

对于永久关系的相关表,在更新、插入或删除记录时,如果只改其一,就会影响数据的完整性。对于更新、插入或删除表间数据的完整性,统称为参照完整性。

对于外键约束,插入参照完整性被满足。因此,如前述:

UserID uint `sql:"type:integer REFERENCES users(id) on update no action on delete no action"`

定义好on update 和 on delete的参数,就可以满足参照完整性。

具体改为:

UserID uint `sql:"type:integer REFERENCES users(id) on update cascade on delete cascade"`

即可,而且数据库还允许有别的选择,这里是级联更新和级联删除,主表已删除,子表就跟着删,这是数据库参照完整性的原初定义。

ps. gorm不默认实施参照完整性,不加约束的原因查了其git issue,主要是因为postgresql要求被关联的表要先存在。而这会导致创建表和自动升级表migration的顺序依赖,所以用户要sqltag或者调用api手动实施。

4、*1对多 和 多对多关系

这不属于完整性范畴。

4.1 1对多

1对多不需要实施完整性约束,因为用户可以对应0到多个产品。因此,表结构里无需添加额外的约束。

type Product struct {
 Code string //`gorm:"primary_key"`
 Price uint
  //UserID uint `sql:"type:integer constraint ref REFERENCES users(id) on update no action on delete no action check(code!='')"`
  //UserCode string //`sql:"type:bigint constraint ref REFERENCES users(code) on update no action on delete cascade"`
  //UserCode string
  //UserID uint
  //User User //`gorm:"foreignkey:UserID;association_foreignkey:ID"`//`gorm:"foreignkey:UserID;association_foreignkey:ID"`
 gorm.Model
 UserID uint
}
type User struct {
 Code string //`gorm:"primary_key"`
 Name string
 gorm.Model
 Products []Product
  //Product Product //`gorm:"EMBEDDED"`
}

上面是gorm一对多的典型定义,users表不会多任何字段,product表会多user_id字段。这里UserID是外键。也可以显式定义,foreignkey 和Association ForeignKey 上例相当于:

type Product struct {
 Code string //`gorm:"primary_key"`
 Price uint
  //UserID uint `sql:"type:integer constraint ref REFERENCES users(id) on update no action on delete no action check(code!='')"`
  //UserCode string //`sql:"type:bigint constraint ref REFERENCES users(code) on update no action on delete cascade"`
  //UserCode string
  //UserID uint
  //User User //`gorm:"foreignkey:UserID;association_foreignkey:ID"`//`gorm:"foreignkey:UserID;association_foreignkey:ID"`
 gorm.Model
 UserID uint
}
type User struct {
 Code string //`gorm:"primary_key"`
 Name string
 gorm.Model
 Products []Product `gorm:"foreignkey:UserID"`
  //Product Product //`gorm:"EMBEDDED"`
}

4.2 多对多

在关系型数据库中,多对多关系需要多一张表,总共3张表,完整性grom是如何保证的?

type Product struct {
 Code string //`gorm:"primary_key"`
 Price uint
  //UserID uint `sql:"type:integer constraint ref REFERENCES users(id) on update no action on delete no action check(code!='')"`
  //UserCode string //`sql:"type:bigint constraint ref REFERENCES users(code) on update no action on delete cascade"`
  //UserCode string
  //UserID uint
  //User User //`gorm:"foreignkey:UserID;association_foreignkey:ID"`//`gorm:"foreignkey:UserID;association_foreignkey:ID"`
 gorm.Model
  //UserID uint
}
type User struct {
 Code string //`gorm:"primary_key"`
 Name string
 gorm.Model
 Products []Product `gorm:"many2many:user_language"`
  //Product Product //`gorm:"EMBEDDED"`
}

此时,会多一个表(jointtable连接表):

CREATE TABLE "user_language" ("user_id" integer,"product_id" integer, PRIMARY KEY ("user_id","product_id"))

products和users表的主键,被联合作为新表的主键。在新表中,user_id和product_id也是外键,在Gorm中,是可以在many2many关系中自定义外键、关联外键的。当然,外键约束就不要想了。

那么,在上例中,按照grom的语法,对于Products成员,外键和关联外键分别是什么呢?简言之,在gorm所有情况下,将嵌入结构体和其父结构体关联起来的那个字段,就是外键;关联外键是写入外键的值的来源对应的键,通常就是父结构体的主键。在多对多情况下,如上例,连接表的user_id是外键,而写入时,并没有将user_id写入Products []Product,写入的是product_id代表的数据,因此product_id是associate_foreignkey,这是gorm的约定,很费解,解释也牵强。

下面是多对多自引用:

type User struct {
 gorm.Model
 Friends []*User `gorm:"many2many:friendships;association_jointable_foreignkey:friend_id"`
}

用association_jointable_foreignkey在连接表里创建了一个字段。也比较费解。

综上:

1、字段的基本约束,通过gorm tag基本都可以设置。

2、gorm支持实体完整性约束。

3、域完整性约束中,外键约束需要通过 sql tag或调用api实现,check约束可以直接在字段上定义。

4、参照完整性gorm不能默认实现,必须通过sql tag或者调用api实现。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。如有错误或未考虑完全的地方,望不吝赐教。

(0)

相关推荐

  • golang gorm 计算字段和获取sum()值的实现

    计算表lb_ytt_user_money_log 中,字段money的和 代码如下: var total_money []int sqlstr := `select SUM(money) as total_money from lb_ytt_user_money_log where user_id = ? and l_type = 1 and status=1 and (create_time> ? and create_time <= ?)` Db.Raw(sqlstr, userID, b

  • golang常用库之操作数据库的orm框架-gorm基本使用详解

    golang常用库:gorilla/mux-http路由库使用 golang常用库:配置文件解析库-viper使用 golang常用库:操作数据库的orm框架-gorm基本使用 一:字段映射-模型定义 gorm中通常用struct来映射字段. gorm教程中叫模型定义 比如我们定义一个模型Model: type User struct { gorm.Model UserId int64 `gorm:"index"` //设置一个普通的索引,没有设置索引名,gorm会自动命名 Birth

  • golang gorm多条件筛选查询操作

    案例: 查看陌陌的动态,依次可以按照发布时间,性别,城市进行筛选 如图进行筛选 gorm链式操作 Method Chaining,Gorm 实现了链式操作接口,所以你可以把代码写成这样: // 创建一个查询 tx := db.Where("name = ?", "jinzhu") // 添加更多条件 if someCondition { tx = tx.Where("age = ?", 20) } else { tx = tx.Where(&qu

  • gorm golang 并发连接数据库报错的解决方法

    底层报错 error:cannot assign requested address 原因 并发场景下 client 频繁请求端口建立tcp连接导致端口被耗尽 解决方案 root执行即可 sysctl -w net.ipv4.tcp_timestamps=1 开启对于TCP时间戳的支持,若该项设置为0,则下面一项设置不起作用 sysctl -w net.ipv4.tcp_tw_recycle=1 表示开启TCP连接中TIME-WAIT sockets的快速回收 以上这篇gorm golang 并

  • golang gorm中格式化时间问题详解

    前言 最近在开发项目时遇到了发现一个问题, gorm虽然可以自动帮你维护 created_at.updated_at.deleted_at这些关键时间字段.但是其原理与弊端需要了解一下. 1.使用方法 通过自定义一个localtime的结构,来控制时间的格式 package utils import ( "time" //"strconv" "fmt" "database/sql/driver" "strconv&q

  • Golang 使用gorm添加数据库排他锁,for update

    适用于先读后更新的数据竞争场景,且应该将加锁操作放到事务中,防止锁被自动释放,原因参考mysql doc func UpdateUser(db *gorm.DB, id int64) error { tx := db.Begin() defer func() { if r := recover(); r != nil { tx.Rollback() } }() if err := tx.Error; err != nil { return err } user := User{} // 锁住指定

  • golang gorm 结构体的表字段缺省值设置方式

    我就废话不多说了,大家还是直接看代码吧~ type Animal struct { ID int64 Name string `gorm:"default:'galeone'"` Age int64 } 把 name 设置上缺省值 galeone 了. 补充:Golang 巧用构造函数设置结构体的默认值 看代码吧~ package main import "fmt" type s1 struct { ID string s2 s2 s3 s3 } type s2 s

  • golang Gorm与数据库完整性约束详解

    数据库约束要点: 主键约束(非空且唯一)外键约束 子表外键字段的值必须在主表被参照字段值得范围内,或者为NULL:外键参照的必须是主表的主键或唯一键:主表主键/唯一键被子表参照时,主表相应记录不允许被删除 在golang中,采用orm对数据库进行建模是比较方便的.grom是其中一个比较流行的orm工具. 本篇基于golang.grom1.91.和PostgreSQL来进行说明. 注:本文的例子是极端情况,一般情况只是单字段主键. 1.实体完整性: 每个关系(表)有且仅有一个主键,每一个主键值必须

  • 基于GORM实现CreateOrUpdate方法详解

    目录 正文 GORM 写接口原理 Create Save Update & Updates FirstOrInit FirstOrCreate 方案一:FirstOrCreate + Assign 方案二:Upsert 总结 正文 CreateOrUpdate 是业务开发中很常见的场景,我们支持用户对某个业务实体进行创建/配置.希望实现的 repository 接口要达到以下两个要求: 如果此前不存在该实体,创建一个新的: 如果此前该实体已经存在,更新相关属性. 根据笔者的团队合作经验看,很多

  • Golang分布式应用之Redis示例详解

    目录 正文 分布式锁 运行测试 分布式过滤器 运行测试 分布式限流器 运行测试 其他 正文 Redis作是一个高性能的内存数据库,常被应用于分布式系统中,除了作为分布式缓存或简单的内存数据库还有一些特殊的应用场景,本文结合Golang来编写对应的中间件. 本文所有代码见github.com/qingwave/go… 分布式锁 单机系统中我们可以使用sync.Mutex来保护临界资源,在分布式系统中同样有这样的需求,当多个主机抢占同一个资源,需要加对应的“分布式锁”. 在Redis中我们可以通过s

  • MySQL学习之完整性约束详解

    数据完整性指的是数据的一致性和正确性.完整性约束是指数据库的内容必须随时遵守的规则.若定义了数据完整性约束,MySQL会负责数据的完整性,每次更新数据时,MySQL都会测试新的数据内容是否符合相关的完整性约束条件,只有符合完整性的约束条件的更新才被接受. 1.主键约束 主键就是表中的一列或多个列的组合,其值能唯一地标识表中的每一行.MySQL为主键列创建唯一性索引,实现数据的唯一性.在查询中使用主键时,该索引可用来对数据进行快速访问.通过定义PRIMARY KEY约束来创建主键,而且PRIMAR

  • django基础之数据库操作方法(详解)

    Django 自称是"最适合开发有限期的完美WEB框架".本文参考<Django web开发指南>,快速搭建一个blog 出来,在中间涉及诸多知识点,这里不会详细说明,如果你是第一次接触Django ,本文会让你在感性上对Django有个认识,完成本文操作后会让你有兴趣阅读的相关书籍和文档. 本文客操作的环境,如无特别说明,后续都以下面的环境为基础: =================== Windows 7/10 python 3.5 Django 1.10 ======

  • C#编程实现连接SQL SERVER数据库实例详解

    本文实例讲述了C#编程实现连接SQL SERVER数据库.分享给大家供大家参考,具体如下: 首先,在SQL SEVER里建立一个名为"Exercise"的数据库名,在该数据库下建立一张名为"lianxi"的表.好,现在开始写代码. 在FORM1里拖一个DATAGIRDVIEW用于显示表,在放一个BUTTON用于执行操作.加入命名空间 复制代码 代码如下: using system.data.sqlclient; private void button1_Click(

  • C++利用MySQL API连接和操作数据库实例详解

    1.C++连接和操作MySQL的方式 系列文章: MySQL 设计和命令行模式下建立详解 C++利用MySQL API连接和操作数据库实例详解 在Windows平台,我们可以使用ADO.ODBC或者MySQL API进行连接和操作.ADO (ActiveX Data Objects,ActiveX数据对象)是Microsoft提出的一个用于存取数据源的COM组件.它提供了程序语言和统一数据访问方式OLE DB的一个中间层,也就是Microsoft提出的应用程序接口(API)用以实现访问关系或非关

  • Java连接操作Oracle数据库代码详解

    废话不多说了,直接给大家贴关键代码了,具体代码如下所示: package com.sp.test; import java.sql.*; import java.util.*; public class Text_lianxi extends Thread { public void run() { try { yunxing(); Thread.sleep(10000); } catch (InterruptedException e) { // TODO 自动生成的 catch 块 e.pr

  • 对Golang import 导入包语法详解

    package 的导入语法 写 Go 代码的时经常用到 import 这个命令用来导入包,参考如下: import( "fmt" ) 然后在代码里面可以通过如下的方式调用: fmt.Println( "我爱北京天安门" ) fmt 是 Go 的标准库,它其实是去 GOROOT 下去加载该模块,当然 Go 的 import 还支持如下两种方式来加载自己写的模块: 相对路径 import "./model" // 当前文件同一目录的 model 目录

  • golang 切片截取参数方法详解

    以 s := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}为例 0. 建议:做slice截取时建议用两个参数,尤其是从底层数组进行切片操作时,因为这样在进行第一次append操作时,会给切片重新分配空间,这样减少切片对数组的影响. 1. 结论:s = s[low : high : max] 切片的三个参数的切片截取的意义为 low为截取的起始下标(含), high为窃取的结束下标(不含high),max为切片保留的原切片的最大下标(不含max):即新切片从老切片的low

随机推荐