1. Home
  2. Docs
  3. golang
  4. ent

ent

Ent 是 Fackbook 开源的一个简单易用的 Database 实体框架。

常用命令

格式: ent init [表名字] 生成文件命令格式(表名必须驼峰命名)

示例: ent init OrderGoods

FQA

出现错误 unsupported Scan, storing driver.Value type []uint8 into type *time.Time

配置数据库源增加参数parseTime

test:test@tcp(192.168.99.99:3306)/goSeckill?parseTime=true

如何删除生成的文件?

假如使用命令 ent init Goods 生成这个实体文件 但现在不想要了

  1. 删除$projcet/ent/schema/goods.go
  2. 然后执行命令 go generate .\ent
  3. 手动删除 $project/ent/goods* 相关文件 和 $project/ent/goods文件夹

如何自定义数据库类型?

$project/schema/goods

假如现在需要定义MySQL的类型是decimal

func (Goods) Fields() []ent.Field {
    return []ent.Field {
        field.Int64("id").Positive(),
        field.Float("goods_price").SchemaType(map[string]string{
            dialect.MySQL: "decimal(10,2)",
        }),
    }
}

相关资料

entgo官网

ent-github

Was this article helpful to you? Yes No

How can we help?