documentation for Ecto.Schema
关于 :virtual
选项如下:
:virtual
- When true, the field is not persisted to the database.
embedded_schema/1
函数表示它描述了一个专门保存在内存中的模式。 (即它永远不会持久化到数据库中。)这是否意味着对于嵌入式模式, :virtual
选项无效?
最佳答案
关于 Ecto.Schema.#embedded_schema/1
的文档实际上说:
An embedded schema is either embedded into another schema or kept exclusively in memory. For this reason, an embedded schema does not require a source name and it does not include a metadata field.
当嵌入到另一个架构中时,它充当直接注入(inject)到目标架构中的代码片段,并且 :virtual
字段照常运行。
当保存在内存中时,:virtual
字段仍然有效
- 与其他字段不同,通过声明类型
:any
允许选择性跳过类型检查 - 未在
__schema__(:fields)
中列出 - 不支持使用
__schema__(:type, field)查询类型
- 不支持
:autogenerate
也不支持:read_after_writes
选项
关于elixir - `virtual: true` 对嵌入式架构有影响吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63291589/