代码之家  ›  专栏  ›  技术社区  ›  toraritte

“exto.Schema.Metadata”的目的是什么(i、 例如,`\u meta\`字段)

  •  2
  • toraritte  · 技术社区  · 7 年前

    Ecto Schema Changeset 仅用于验证,而不将任何内容持久化到数据库并尝试确定是否应该使用 Ecto.Schema.schema/2 Ecto.Schema.embedded_schema/1 嵌入式架构不需要源名称,也不包含元数据字段。 "

    embedded_schema/1 ,效果很好,但这让我很疑惑 这个 Ecto.Schema.Metadata docs 对澄清这一点没有多大帮助:

    字段包括:

    • state—结构生命周期中的状态,其中一个:build,:loaded,
    • source—查询旁架构的源 前缀,默认为{nil,“source”}
    • context—由 数据库

    正在搜索“ Ecto.Schema docs ,在上面引用的 嵌入式架构/1 .


    更新

    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.
    

    )

    2 回复  |  直到 7 年前
        1
  •  1
  •   Aleksei Matiushkin    7 年前

    Ecto.Schema.Metadata 仅用于存储所有与数据库相关的信息。

    series of posts on Ecto 2 → 3 ,

    自exto2.0以来,越来越多的开发人员和团队使用exto进行数据映射和验证,而不需要数据库。然而,向应用程序中添加exto仍然会带来很多SQL负担,比如适配器、沙盒和迁移,许多人认为这是一个混合消息。

    后者是关于元数据的。

    有一个经验法则的异位2:你是否需要数据库 在后面,使用 schema embedded_schema 否则。


    我的一般建议是,当你想简单地理解一些东西的时候,不要读文档, read the code .

        2
  •  0
  •   Sheharyar    7 年前

    这个 __meta__ 字段由exto在内部用于维护有关记录、关联(如果它们已加载、过时或更久)的元数据。

    存储结构的元数据。

    • state :built , :loaded :deleted
    • source 默认为 {nil, "source"}
    • context -数据库存储的上下文