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