ent

ent

  • Docs
  • GoDoc
  • Github
  • Blog

›Schema

Getting Started

  • Quick Introduction

Schema

  • Introduction
  • Fields
  • Edges
  • Indexes
  • Mixin
  • Annotations

Code Generation

  • Introduction
  • CRUD API
  • Graph Traversal
  • Eager Loading
  • Hooks
  • Privacy
  • Transactions
  • Predicates
  • Aggregation
  • Paging And Ordering

Migration

  • Database Migration
  • Supported Dialects

Misc

  • External Templates
  • GraphQL Integration
  • sql.DB Integration
  • Testing
  • FAQ
  • Feature Flags

Introduction

Quick Summary

Schema describes the definition of one entity type in the graph, like User or Group, and can contain the following configurations:

  • Entity fields (or properties), like: name or age of a User.
  • Entity edges (or relations), like: User's groups, or User's friends.
  • Database specific options, like: indexes or unique indexes.


Here's an example of a schema:

package schema

import (
    "entgo.io/ent"
    "entgo.io/ent/schema/field"
    "entgo.io/ent/schema/edge"
    "entgo.io/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(),
    }
}

Entity schemas are usually stored inside ent/schema directory under the root directory of your project, and can be generated by entc as follows:

go run entgo.io/ent/cmd/ent init User Group

It's Just Another ORM

If you are used to the definition of relations over edges, that's fine. The modeling is the same. You can model with ent whatever you can model with other traditional ORMs. There are many examples in this website that can help you get started in the Edges section.

← Quick IntroductionFields →
  • Quick Summary
  • It's Just Another ORM
Docs
Getting StartedSchema GuideCode GenerationSchema Migration
Legal
PrivacyTerms
Credit
The Go gopher was designed by Renee French. The design is licensed under the Creative Commons 3.0 Attributions license. Read this article for more details.

Design by Moriah Rich, illustration by Ariel Mashraki.
Facebook Open Source
Copyright 2021 Facebook Inc.