虚拟模型
Virtual models in ecto
有没有一种方法可以在 ecto.xml 中定义虚拟模型。其中没有 migration file
和 don't persist in the data base
。我没有在 Ecto 文档中找到任何与此相关的文档。我需要这个来测试 iex 中的一些功能。使用与应用程序的其余部分分开的模型进行测试。
我发现 embedded schema
其中包含的字段不会保留在数据库中,但与模型无关。
任何帮助将不胜感激。
嵌入式架构就好了。
defmodule Test.Model do
@moduledoc ~S"""
The dummy test model that is not stored in the database.
"""
use Ecto.Schema
import Ecto.Changeset
@type t :: Ecto.Schema.t
@fields ~w|foo bar baz|a
@primary_key false
embedded_schema do
field :foo, :string
field :bar, :integer
field :baz, :float
end
def new(data) when is_map(data) do
%__MODULE__{}
|> cast(data, @fields)
|> validate_required(~w|foo|a)
|> apply_changes()
end
end
一旦定义,它就可以用作普通模式。
有没有一种方法可以在 ecto.xml 中定义虚拟模型。其中没有 migration file
和 don't persist in the data base
。我没有在 Ecto 文档中找到任何与此相关的文档。我需要这个来测试 iex 中的一些功能。使用与应用程序的其余部分分开的模型进行测试。
我发现 embedded schema
其中包含的字段不会保留在数据库中,但与模型无关。
任何帮助将不胜感激。
嵌入式架构就好了。
defmodule Test.Model do
@moduledoc ~S"""
The dummy test model that is not stored in the database.
"""
use Ecto.Schema
import Ecto.Changeset
@type t :: Ecto.Schema.t
@fields ~w|foo bar baz|a
@primary_key false
embedded_schema do
field :foo, :string
field :bar, :integer
field :baz, :float
end
def new(data) when is_map(data) do
%__MODULE__{}
|> cast(data, @fields)
|> validate_required(~w|foo|a)
|> apply_changes()
end
end
一旦定义,它就可以用作普通模式。