我想在我的Ecto模型中定义一个PostgreSQL-Hash类型字段,但我不知道该怎么做。我没有找到关于这个主题的明确指南,我假设它隐藏在这里:http://hexdocs.pm/ecto/Ecto.Schema.html
有没有人明确指导在Ecto中做PostgreSQL-Hash字段?
答案 0 :(得分:4)
正如JustMichael所说,答案是:map
类型。
表格定义:
CREATE TABLE public.authors (
id INTEGER PRIMARY KEY NOT NULL DEFAULT nextval('authors_id_seq'::regclass),
settings HSTORE,
updated_at TIMESTAMP WITHOUT TIME ZONE,
inserted_at TIMESTAMP WITHOUT TIME ZONE
);
Ecto模型:
defmodule Nexus.Author do
use Nexus.Web, :model
schema "authors" do
field :settings, :map
timestamps
end
end
现在我可以访问设置Nexus.Repo.get(Author, some_id).settings
并获得一张地图作为回报。