Jos*_*itt 38 postgresql ecto phoenix-framework
我想使用Postgres使用Ecto存储一个浮点值数组.我正在使用Ecto与Phoenix Framework和Elixir.
我如何为此定义模型和迁移?
我没有尝试太多,除了搜索网络,没有找到任何有用的东西:-(
我尝试使用如下模式定义模型:
schema "my_model" do
field :my_array, :array
timestamps
end
Run Code Online (Sandbox Code Playgroud)
出现错误"无效或未知类型:字段数组:my_array"
Jos*_*itt 60
我在这里找到了Ecto.Schema的基本类型列表中的答案:
答案是定义这样的类型:
schema "my_model" do
field :my_array, {:array, :float}
timestamps
end
Run Code Online (Sandbox Code Playgroud)
Bar*_*ira 28
正如您所写,使用Ecto.Schema中的数组类型
在模型中:
schema "my_model" do
field :my_array, {:array, inner_type}
end
Run Code Online (Sandbox Code Playgroud)
@neildaemond迁移:
alter table(:my_models) do
add :my_array, {:array, inner_type}
end
Run Code Online (Sandbox Code Playgroud)
替换inner_type为其中一种有效类型,例如:string.
您也可以使用map类型执行相同的操作:
schema "my_model" do
field :my_map, {:map, inner_type}
end
Run Code Online (Sandbox Code Playgroud)