我计划使用 Ecto
的 Schema
和 Changeset
仅用于验证而不将任何内容保存到数据库,并试图弄清楚我是否应该使用 Ecto.Schema.schema/2
或 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.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 .
关于elixir - `Ecto.Schema.Metadata` 的目的是什么? (即 `__meta__` 字段),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52799805/