`Ecto.Schema.Metadata` 的目的是什么? (即 `__meta__` 字段)
What is the purpose of the `Ecto.Schema.Metadata`? (i.e., the `__meta__` field)
我计划使用 Ecto
的 Schema
和 Changeset
仅用于验证而不将任何内容保存到数据库,并试图弄清楚我是否应该使用 Ecto.Schema.schema/2
or Ecto.Schema.embedded_schema/1
.根据文档,它们之间的唯一区别是“嵌入式模式不需要源名称,也不包含元数据字段。”
所以我选择了 embedded_schema/1
,效果很好,但它让我想知道 元数据到底有什么用? Ecto.Schema.Metadata
docs 并不多帮助澄清这一点:
Stores metadata of a struct.
The fields are:
- state - the state in a struct’s lifetime, one of :built, :loaded,
:deleted
- source - the source for the schema alongside the query
prefix, defaults to {nil, "source"}
- context - context stored by the
database
搜索“meta”没有结果,“metadata”返回 Ecto.Schema
docs 中的一个结果,在上面引用的行中embedded_schema/1
.
更新
忘了 Ecto 3 即将推出,Hexdocs 文档仍然适用于 Ecto 2.2.11。在源代码中找到 the updated Metadata
docs,但更冗长:
Stores metadata of a struct.
## State
The state of the schema is stored in the `:state`
field and allows following values:
* `:built` - the struct was constructed in
memory and is not persisted
to database yet;
* `:loaded` - the struct was loaded from database
and represents persisted data;
* `:deleted` - the struct was deleted and no longer
represents persisted data.
## Source
The `:source` tracks the (table or collection) where
the struct is or should be persisted to.
## Prefix
Tracks the source prefix in the data storage.
## Context
The `:context` field represents additional state some
databases require for proper updates of data. It is
not used by the built-in adapters of `Ecto.Adapters.Postres`
and `Ecto.Adapters.MySQL`.
## Schema
The `:schema` field refers the module name for the
schema this metadata belongs to.
(The updated Schema
docs也解决了我上面的困境:
An Ecto schema is used to map any data source into an Elixir struct.
The definition of the schema is possible through two main APIs:
`schema/2` and `embedded_schema/1`.
`schema/2` is typically used to map data from a persisted source,
usually a database table, into Elixir structs and vice-versa. For
this reason, the first argument of `schema/2` is the source (table)
name. Structs defined with `schema/2` also contain a `__meta__` field
with metadata holding the status of the struct, for example, if it
has been built, loaded or deleted.
On the other hand, `embedded_schema/1` is used for defining schemas
that are embedded in other schemas or only exist in-memory. For example,
you can use such schemas to receive data from a command line interface
and validate it, without ever persisting it elsewhere. Such structs
do not contain a `__meta__` field, as they are never persisted.
)
Ecto 在内部使用 __meta__
字段来维护有关记录、关联的元数据,如果它们已加载、过时或更多。
您链接的文档中的描述似乎是自给自足的:
Stores metadata of a struct.
The fields are:
state
- the state in a struct's lifetime, one of :built
,
:loaded
, :deleted
source
- the source for the schema alongside the query prefix,
defaults to {nil, "source"}
context
- context stored by the database
Ecto.Schema.Metadata
仅用于存储所有与数据库相关的信息。
正如何塞在 series of posts on Ecto 2 → 3 中提到的,
Since Ecto 2.0, an increased number of developers and teams have been using Ecto for data mapping and validation, without a need for a database. However, adding Ecto to your application would still bring a lot of the SQL baggage, such as adapters, sandboxes and migrations, which many considered to be a mixed message.
后者是关于元数据的。
Ecto 2有一个经验法则:是否需要后面的DBtable,就用schema
;否则使用 embedded_schema
。
旁注:我的一般建议是当您想简明扼要地理解某些内容时,不要阅读文档,read the code。
我计划使用 Ecto
的 Schema
和 Changeset
仅用于验证而不将任何内容保存到数据库,并试图弄清楚我是否应该使用 Ecto.Schema.schema/2
or Ecto.Schema.embedded_schema/1
.根据文档,它们之间的唯一区别是“嵌入式模式不需要源名称,也不包含元数据字段。”
所以我选择了 embedded_schema/1
,效果很好,但它让我想知道 元数据到底有什么用? Ecto.Schema.Metadata
docs 并不多帮助澄清这一点:
Stores metadata of a struct.
The fields are:
- state - the state in a struct’s lifetime, one of :built, :loaded, :deleted
- source - the source for the schema alongside the query prefix, defaults to {nil, "source"}
- context - context stored by the database
搜索“meta”没有结果,“metadata”返回 Ecto.Schema
docs 中的一个结果,在上面引用的行中embedded_schema/1
.
更新
忘了 Ecto 3 即将推出,Hexdocs 文档仍然适用于 Ecto 2.2.11。在源代码中找到 the updated Metadata
docs,但更冗长:
Stores metadata of a struct.
## State
The state of the schema is stored in the `:state`
field and allows following values:
* `:built` - the struct was constructed in
memory and is not persisted
to database yet;
* `:loaded` - the struct was loaded from database
and represents persisted data;
* `:deleted` - the struct was deleted and no longer
represents persisted data.
## Source
The `:source` tracks the (table or collection) where
the struct is or should be persisted to.
## Prefix
Tracks the source prefix in the data storage.
## Context
The `:context` field represents additional state some
databases require for proper updates of data. It is
not used by the built-in adapters of `Ecto.Adapters.Postres`
and `Ecto.Adapters.MySQL`.
## Schema
The `:schema` field refers the module name for the
schema this metadata belongs to.
(The updated Schema
docs也解决了我上面的困境:
An Ecto schema is used to map any data source into an Elixir struct.
The definition of the schema is possible through two main APIs:
`schema/2` and `embedded_schema/1`.
`schema/2` is typically used to map data from a persisted source,
usually a database table, into Elixir structs and vice-versa. For
this reason, the first argument of `schema/2` is the source (table)
name. Structs defined with `schema/2` also contain a `__meta__` field
with metadata holding the status of the struct, for example, if it
has been built, loaded or deleted.
On the other hand, `embedded_schema/1` is used for defining schemas
that are embedded in other schemas or only exist in-memory. For example,
you can use such schemas to receive data from a command line interface
and validate it, without ever persisting it elsewhere. Such structs
do not contain a `__meta__` field, as they are never persisted.
)
Ecto 在内部使用 __meta__
字段来维护有关记录、关联的元数据,如果它们已加载、过时或更多。
您链接的文档中的描述似乎是自给自足的:
Stores metadata of a struct.
The fields are:
state
- the state in a struct's lifetime, one of:built
,:loaded
,:deleted
source
- the source for the schema alongside the query prefix, defaults to{nil, "source"}
context
- context stored by the database
Ecto.Schema.Metadata
仅用于存储所有与数据库相关的信息。
正如何塞在 series of posts on Ecto 2 → 3 中提到的,
Since Ecto 2.0, an increased number of developers and teams have been using Ecto for data mapping and validation, without a need for a database. However, adding Ecto to your application would still bring a lot of the SQL baggage, such as adapters, sandboxes and migrations, which many considered to be a mixed message.
后者是关于元数据的。
Ecto 2有一个经验法则:是否需要后面的DBtable,就用schema
;否则使用 embedded_schema
。
旁注:我的一般建议是当您想简明扼要地理解某些内容时,不要阅读文档,read the code。