elixir - `Ecto.Schema.Metadata` 的目的是什么? (即 `__meta__` 字段)

标签 elixir schema ecto

我计划使用 EctoSchemaChangeset 仅用于验证而不将任何内容保存到数据库,并试图弄清楚我是否应该使用 Ecto.Schema.schema/2Ecto.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/

相关文章:

Elixir 脚本递归加载文件夹中的所有模块

Elixir - 按名称获取主机?

macros - 将变量注入(inject)到带引号的表达式中

mysql - 应该使用 id 还是 timestamp 来确定数据库表中行的创建顺序? (考虑到系统时钟设置不正确的可能性)

php - 什么是查询 "Facebook Wall"功能的良好数据库架构和方法

elixir - 测试 Elixir Ecto 模型关联的最佳实践

postgresql - Ecto - 在单个查询中返回带有按日期分组的时间戳的记录

testing - Elixir - 在较大的位串中找到子位串

sql - 如何为组 SQL Server 分配一个 id

elixir - 如何使用 Ecto 构建 WHERE IN 数组子句?