ent 提供了自动生成schema 但是,我们可以基于生成schema 进行扩展,schema 主要包含以下配置

  • 实体的字段(或者属性)比如 user 的name 以及age
  • 实体的边(关系),比如user 的groups user 的friends
  • 数据库选项,所以以及唯一索引

一个简单的schema

package schema
import (
    "github.com/facebookincubator/ent"
    "github.com/facebookincubator/ent/schema/field"
    "github.com/facebookincubator/ent/schema/edge"
    "github.com/facebookincubator/ent/schema/index"
)
type User struct {
    ent.Schema
}
func (User) Fields() []ent.Field {
    return []ent.Field{
        field.Int("age"),
        field.String("name"),
        field.String("nickname").
            Unique(),
    }
}
func (User) Edges() []ent.Edge {
    return []ent.Edge{
        edge.To("groups", Group.Type),
        edge.To("friends", User.Type),
    }
}
func (User) Index() []ent.Index {
    return []ent.Index{
        index.Fields("age", "name").
            Unique(),
    }
}
 

说明

ent 提供了一个命令行工具,我们可以用来生成schema

entc init User Group

附带ent 命令行工具的帮助

Usage:
  entc [command]
Available Commands:
  describe print a description of the graph schema
  generate generate go code for the schema directory
  help Help about any command
  init initialize an environment with zero or more schemas
Flags:
  -h, --help help for entc
Use "entc [command] --help" for more information about a command.
 
 

参考资料

https://entgo.io/docs/schema-def/

最新文章

  1. 该不该用inline-block取代float? inline和float的区别?
  2. 各种 starter poms (启动器)
  3. codevs 1204 寻找子串位置
  4. Java提高篇---Stack
  5. linux c 生成uuid
  6. shell脚本初识
  7. cics下任务的停止
  8. POJ 2513 Colored Sticks - from lanshui_Yang
  9. MyBatis 注解
  10. android面试题2
  11. 深入struts2.0(五)--Dispatcher类
  12. eclipse下启动tomcat项目,访问tomcat默认端口显示404错误
  13. Gmail,QMail,163邮箱的 IMAP/SMTP/POP3 地址
  14. VC++读取图像RGB值
  15. 一键安装 redmine on windows 和发邮件设置
  16. 微信小程序开发之初探
  17. C++ delegate的几种方法
  18. WCF Restful Service Get / Post请求
  19. 9.22 keep studying
  20. Maven入门-安装及配置(一)

热门文章

  1. Django 定义视图函数
  2. TCP/UDP的网络底层实现
  3. js 加密方法Encrypt
  4. C# 创建json传输格式的http请求
  5. HTML转换特效相关插件
  6. .NET中的泛型概述
  7. tiny-spring 分析
  8. Beego 学习笔记三:Beego业务逻辑
  9. JavaScript 函数(一)
  10. JavaScript 之 RegExp 对象