beego orm的使用
在使用beego model 去操作数据库时 有一些疑惑 找到了一个比较好的博文
原文地址 : https://my.oschina.net/u/252343/blog/829912 (KelvinQ )侵删
模型定义
复杂的模型定义不是必须的,此功能用作数据库数据转换和自动建表
默认的表名规则,使用驼峰转蛇形:
- AuthUser -> auth_user
- Auth_User -> auth__user
- DB_AuthUser -> d_b__auth_user
除了开头的大写字母以外,遇到大写会增加 _
,原名称中的下划线保留。
自定义表名
- type User struct {
- Id int
- Name string
- }
- func (u *User) TableName() string {
- return "auth_user"
- }
如果前缀设置为prefix_
那么表名为:prefix_auth_user
自定义索引
为单个或多个字段增加索引
- type User struct {
- Id int
- Name string
- Email string
- }
- // 多字段索引
- func (u *User) TableIndex() [][]string {
- return [][]string{
- []string{"Id", "Name"},
- }
- }
- // 多字段唯一键
- func (u *User) TableUnique() [][]string {
- return [][]string{
- []string{"Name", "Email"},
- }
- }
自定义引擎
仅支持 MySQL
默认使用的引擎,为当前数据库的默认引擎,这个是由你的 mysql 配置参数决定的。
你可以在模型里设置 TableEngine 函数,指定使用的引擎
- type User struct {
- Id int
- Name string
- Email string
- }
- // 设置引擎为 INNODB
- func (u *User) TableEngine() string {
- return "INNODB"
- }
设置参数
orm:"null;rel(fk)"
多个设置间使用 ;
分隔,设置的值如果是多个,使用 ,
分隔。
忽略字段
设置 -
即可忽略 struct 中的字段
- type User struct {
- ...
- AnyField string `orm:"-"`
- ...
- }
auto
当 Field 类型为 int, int32, int64, uint, uint32, uint64 时,可以设置字段为自增健
- 当模型定义里没有主键时,符合上述类型且名称为
Id
的 Field 将被视为自增健。
鉴于 go 目前的设计,即使使用了 uint64,但你也不能存储到他的最大值。依然会作为 int64 处理。
参见 issue 6113
pk
设置为主键,适用于自定义其他类型为主键
null
数据库表默认为 NOT NULL
,设置 null 代表 ALLOW NULL
Name string `orm:"null"`
index
为单个字段增加索引
unique
为单个字段增加 unique 键
Name string `orm:"unique"`
column
为字段设置 db 字段的名称
Name string `orm:"column(user_name)"`
size
string 类型字段默认为 varchar(255)
设置 size 以后,db type 将使用 varchar(size)
Title string `orm:"size(60)"`
digits / decimals
设置 float32, float64 类型的浮点精度
Money float64 `orm:"digits(12);decimals(4)"`
总长度 12 小数点后 4 位 eg: 99999999.9999
auto_now / auto_now_add
- Created time.Time `orm:"auto_now_add;type(datetime)"`
- Updated time.Time `orm:"auto_now;type(datetime)"`
- auto_now 每次 model 保存时都会对时间自动更新
- auto_now_add 第一次保存时才设置时间
对于批量的 update 此设置是不生效的
type
设置为 date 时,time.Time 字段的对应 db 类型使用 date
Created time.Time `orm:"auto_now_add;type(date)"`
设置为 datetime 时,time.Time 字段的对应 db 类型使用 datetime
Created time.Time `orm:"auto_now_add;type(datetime)"`
default
为字段设置默认值,类型必须符合(目前仅用于级联删除时的默认值)
- type User struct {
- ...
- Status int `orm:"default(1)"`
- ...
- }
表关系设置
rel / reverse
RelOneToOne:
- type User struct {
- ...
- Profile *Profile `orm:"null;rel(one);on_delete(set_null)"`
- ...
- }
对应的反向关系 RelReverseOne:
- type Profile struct {
- ...
- User *User `orm:"reverse(one)"`
- ...
- }
RelForeignKey:
- type Post struct {
- ...
- User *User `orm:"rel(fk)"` // RelForeignKey relation
- ...
- }
对应的反向关系 RelReverseMany:
- type User struct {
- ...
- Posts []*Post `orm:"reverse(many)"` // fk 的反向关系
- ...
- }
RelManyToMany:
- type Post struct {
- ...
- Tags []*Tag `orm:"rel(m2m)"` // ManyToMany relation
- ...
- }
对应的反向关系 RelReverseMany:
- type Tag struct {
- ...
- Posts []*Post `orm:"reverse(many)"`
- ...
- }
rel_table / rel_through
此设置针对 orm:"rel(m2m)"
的关系字段
- rel_table 设置自动生成的 m2m 关系表的名称
- rel_through 如果要在 m2m 关系中使用自定义的 m2m 关系表
- 通过这个设置其名称,格式为 pkg.path.ModelName
- eg: app.models.PostTagRel
- PostTagRel 表需要有到 Post 和 Tag 的关系
当设置 rel_table 时会忽略 rel_through
设置方法:
orm:"rel(m2m);rel_table(the_table_name)"
orm:"rel(m2m);rel_through(pkg.path.ModelName)"
on_delete
设置对应的 rel 关系删除时,如何处理关系字段。
- cascade 级联删除(默认值)
- set_null 设置为 NULL,需要设置 null = true
- set_default 设置为默认值,需要设置 default 值
- do_nothing 什么也不做,忽略
- type User struct {
- ...
- Profile *Profile `orm:"null;rel(one);on_delete(set_null)"`
- ...
- }
- type Profile struct {
- ...
- User *User `orm:"reverse(one)"`
- ...
- }
- // 删除 Profile 时将设置 User.Profile 的数据库字段为 NULL
关于 on_delete 的相关例子
- type User struct {
- Id int
- Name string
- }
- type Post struct {
- Id int
- Title string
- User *User `orm:"rel(fk)"`
- }
假设 Post -> User 是 ManyToOne 的关系,也就是外键。
o.Filter("Id", 1).Delete()
这个时候即会删除 Id 为 1 的 User 也会删除其发布的 Post
不想删除的话,需要设置 set_null
- type Post struct {
- Id int
- Title string
- User *User `orm:"rel(fk);null;on_delete(set_null)"`
- }
那这个时候,删除 User 只会把对应的 Post.user_id 设置为 NULL
当然有时候为了高性能的需要,多存点数据无所谓啊,造成批量删除才是问题。
- type Post struct {
- Id int
- Title string
- User *User `orm:"rel(fk);null;on_delete(do_nothing)"`
- }
那么只要删除的时候,不操作 Post 就可以了。
模型字段与数据库类型的对应
在此列出 ORM 推荐的对应数据库类型,自动建表功能也会以此为标准。
默认所有的字段都是 NOT NULL
MySQL
go | mysql |
int, int32 - 设置 auto 或者名称为 Id 时 |
integer AUTO_INCREMENT |
int64 - 设置 auto 或者名称为 Id 时 |
bigint AUTO_INCREMENT |
uint, uint32 - 设置 auto 或者名称为 Id 时 |
integer unsigned AUTO_INCREMENT |
uint64 - 设置 auto 或者名称为 Id 时 |
bigint unsigned AUTO_INCREMENT |
bool | bool |
string - 默认为 size 255 | varchar(size) |
string - 设置 type(text) 时 | longtext |
time.Time - 设置 type 为 date 时 | date |
time.Time | datetime |
byte | tinyint unsigned |
rune | integer |
int | integer |
int8 | tinyint |
int16 | smallint |
int32 | integer |
int64 | bigint |
uint | integer unsigned |
uint8 | tinyint unsigned |
uint16 | smallint unsigned |
uint32 | integer unsigned |
uint64 | bigint unsigned |
float32 | double precision |
float64 | double precision |
float64 - 设置 digits, decimals 时 | numeric(digits, decimals) |
Sqlite3
go | sqlite3 |
int, int32, int64, uint, uint32, uint64 - 设置 auto 或者名称为 Id 时 |
integer AUTOINCREMENT |
bool | bool |
string - 默认为 size 255 | varchar(size) |
string - 设置 type(text) 时 | text |
time.Time - 设置 type 为 date 时 | date |
time.Time | datetime |
byte | tinyint unsigned |
rune | integer |
int | integer |
int8 | tinyint |
int16 | smallint |
int32 | integer |
int64 | bigint |
uint | integer unsigned |
uint8 | tinyint unsigned |
uint16 | smallint unsigned |
uint32 | integer unsigned |
uint64 | bigint unsigned |
float32 | real |
float64 | real |
float64 - 设置 digits, decimals 时 | decimal |
PostgreSQL
go | postgres |
int, int32, int64, uint, uint32, uint64 - 设置 auto 或者名称为 Id 时 |
serial |
bool | bool |
string - 默认为 size 255 | varchar(size) |
string - 设置 type(text) 时 | text |
time.Time - 设置 type 为 date 时 | date |
time.Time | timestamp with time zone |
byte | smallint CHECK(“column” >= 0 AND “column” <= 255) |
rune | integer |
int | integer |
int8 | smallint CHECK(“column” >= -127 AND “column” <= 128) |
int16 | smallint |
int32 | integer |
int64 | bigint |
uint | bigint CHECK(“column” >= 0) |
uint8 | smallint CHECK(“column” >= 0 AND “column” <= 255) |
uint16 | integer CHECK(“column” >= 0) |
uint32 | bigint CHECK(“column” >= 0) |
uint64 | bigint CHECK(“column” >= 0) |
float32 | double precision |
float64 | double precision |
float64 - 设置 digits, decimals 时 | numeric(digits, decimals) |
关系型字段
其字段类型取决于对应的主键。
- RelForeignKey
- RelOneToOne
- RelManyToMany
- RelReverseOne
- RelReverseMany
beego orm的使用的更多相关文章
- beego orm 忽略字段
忽略字段 设置 - 即可忽略 struct 中的字段 type User struct { ... AnyField string `orm:"-"` ... } beego or ...
- golang学习笔记16 beego orm 数据库操作
golang学习笔记16 beego orm 数据库操作 beego ORM 是一个强大的 Go 语言 ORM 框架.她的灵感主要来自 Django ORM 和 SQLAlchemy. 目前该框架仍处 ...
- 基于beego orm 针对oracle定制
目前golang的ORM对oracle支持都没有mysql那样完整,一个orm要同时兼容mysql和oracle由于在sql语法上区别,会使整orm变的非常臃肿. 本项目是在beego orm上修改, ...
- Golang beego ORM + CRUP 操作详解
构建beego Web 项目 首先构建一个beego 的web 项目,这个我们完全可以参考beego 官网中的开发文档,上面介绍的非常的详细,在这我就不给大家介绍,主要是介绍ORM ...
- beego orm操作mysql数据库
慢慢弄起来~~ 按官方操作文档试一下. 那个err重复和user编号问题,以后再弄.. package main import ( "fmt" "github.com/a ...
- beego orm mysql
beego框架中的rom支持mysql 项目中使用到mvc模式,总结下使用方式: models中 package models import ( //使用beego orm 必备 "gith ...
- beego——ORM使用方法
先来看一个简单示例: models.gp package main import ( "github.com/astaxie/beego/orm" ) type User stru ...
- beego orm
http://beego.me/docs/mvc/model/overview.md go get github.com/astaxie/beego/orm Simple Usage package ...
- beego框架学习--beego orm映射
什么是ORM 即Object-Relationl Mapping,它的作用是在关系型数据库和对象之间作一个映射,这样,我们在具体的操作数据库的时候,就不需要再去和复杂的SQL语句打交道,只要像平时操作 ...
随机推荐
- 关于 JavaSrcipt 前端开发的建议:模块化开发
JavaScript 是一种优秀的脚本语言. 在 JavaScript 的诞生之初,便于 浏览器 密不可分,如今它更是到了服务器中大展身手. 但是这里不叙述服务端的开发建议. Script 翻译过来就 ...
- Java 程序是如何执行的
Java 程序是如何执行的 了解任何一门语言的精髓都是先俯览其全貌,从宏观的视角把握全局,然后再深入每个知识点逐个击破,这样就可以深入而快速的掌握一项技能.同样学习 Java 也是如此,本节就让我们先 ...
- Spring配置 bean
在 Spring 的 IOC 容器里配置 Bean <bean id="helloWorld" class="com.xiya.spring.beans.Hello ...
- 使用Intellij编写Spring Hello World
编写基于Intellij2016.3与Java SDK1.8 下载Spring最新jar包: http://repo.spring.io/release/org/springframework/spr ...
- Android: Fragment编程指南
本文来自于www.lanttor.org Fragment代表了Activity里的一个行为,或者Activity UI的一部分.你可以在一个activity里构造多个Fragment,也可以在多个a ...
- learn about sqlserver partitition and partition table --- add or remove table partitions
demo/* add partitions */ alter database xxx add filegroup FG_=fff_201708;alter database xxx add file ...
- 【基础】CodeBlocks调试器基本使用方法
CodeBlocks是一个开放源码的全功能的跨平台C/C++集成开发环境. 下载地址:http://www.codeblocks.org/downloads/26 其中,Windows环境下可以使用 ...
- Java DTO(data transfer object)的理解
首先明白springboot每层 model层 model层即数据库实体层,也被称为entity层,pojo层. 一般数据库一张表对应一个实体类,类属性同表字段一一对应. Model层是数据层: Ta ...
- Chrome恢复显示网址 https:// 和 www
文章来自我的博客: https://blog.ljyngup.com/archives/686.html/ Chrome这个新规弄得我也很蛋疼,每次一点输入框就突然突出来一下.后来在Hostloc论坛 ...
- 使用helm安装jenkin和gitlab
一.使用服务介绍 存储: 阿里云NAS k8s网络插件: calico k8s版本: 1.15.2 二.helm安装 https://www.cnblogs.com/zhangb8042/p/1020 ...