[golang] ent使用

复制代码
package schema

import (
	"encoding/json"
	"time"

	"entgo.io/ent"
	"entgo.io/ent/dialect/entsql"
	"entgo.io/ent/schema"
	"entgo.io/ent/schema/field"
)

// Configuration holds the schema definition for the Configuration entity.
type Configuration struct {
	ent.Schema
}

// Annotations of the Configuration.
func (Configuration) Annotations() []schema.Annotation {
	return []schema.Annotation{
		entsql.Annotation{Table: "configurations"},//数据库 明
	}
}

// Fields of the Configuration.
func (Configuration) Fields() []ent.Field {
	return []ent.Field{
		field.Int("id").Unique().Immutable().Positive(),
		field.String("config_key"),
		field.JSON("config_value", json.RawMessage{}),
		field.String("remark"),
		field.Int("version").Default(0),
		field.Time("created_at").Default(time.Now),
	}
}

// Edges of the Configuration.
func (Configuration) Edges() []ent.Edge {
	return []ent.Edge{}
}

go generate ./ent

调用生成 代码

详细可参考 项目创建 | ent

相关推荐
Li_76953221 小时前
Redis进阶(二)—— Redis 事务
数据库·redis·缓存
少云清21 小时前
【接口测试】2_PyMySQL模块 _数据库操作步骤及事务
数据库·pymysql·代码实现
AI题库21 小时前
PostgreSQL 18 从新手到大师:实战指南 - 1.3 Docker环境搭建
数据库·docker·postgresql
少云清21 小时前
【接口测试】3_PyMySQL模块 _连接数据库
数据库·pymysql·代码实现
superman超哥21 小时前
仓颉编译器优化揭秘:尾递归优化的原理与实践艺术
开发语言·后端·仓颉编程语言·仓颉·仓颉语言·尾递归·仓颉编译器
自由生长202421 小时前
保障缓存和数据库尽量一致的策略
后端
lkbhua莱克瓦2421 小时前
基础-SQL-DML
开发语言·数据库·笔记·sql·mysql
saber_andlibert21 小时前
【C++转GO】初阶知识
开发语言·c++·golang
IT艺术家-rookie21 小时前
golang--解决 Go 并发场景下的数据竞争问题的方案
golang
海南java第二人21 小时前
Spring Bean作用域深度解析:从单例到自定义作用域的全面指南
java·后端·spring