> For the complete documentation index, see [llms.txt](https://3ks.gitbook.io/ent-zh_cn/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://3ks.gitbook.io/ent-zh_cn/schema/schema-def.md).

# 介绍

## 简介

模式（Schema）描述了图中一个实体类型，比如 `User` 或 `Group`，并且可以包含以下配置：

* 字段（或属性），例如：`User` 的姓名或年龄。
* 边（或关系），例如：`User` 的群组或 `User` 的朋友。
* 数据库特定的选项，例如：索引和唯一索引。

\
&#x20;这里有一个模式的例子：

```go
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` ，通过下面的命令生成：

```
entc init User Group
```

## ent 只是又一个 ORM

如果您习惯通过边定义关系，这很好。建模是相同的。您可以像使用其他传统 ORM 一样使用 `ent` 进行建模。 这里有很多示例，可以帮助您从 [边](/ent-zh_cn/schema/schema-edges.md) 部分开始。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://3ks.gitbook.io/ent-zh_cn/schema/schema-def.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
