tha*_*way 0 json elixir ecto postgrex
我只是尝试使用Postgrex而没有任何类型的ecto设置,所以只是文档自述文件中的示例.
这是我的模块的样子:
defmodule Receive do
def start(_type, _args) do
{:ok, pid} = Postgrex.start_link(
hostname: "localhost",
username: "john",
# password: "",
database: "property_actions",
extensions: [{Postgrex.Extensions.JSON}]
)
Postgrex.query!(
pid,
"INSERT INTO actions (search_terms) VALUES ($1)",
[
%{foo: 'bar'}
]
)
end
end
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,我得到了
** (RuntimeError) type `json` can not be handled by the types module Postgrex.DefaultTypes, it must define a `:json` library in its options to support JSON types
Run Code Online (Sandbox Code Playgroud)
有什么我没有正确设置?根据我在文档中收集的内容,我甚至不需要具有该扩展行,因为默认情况下处理json.
在Postgrex <= 0.13上,您需要定义自己的类型:
Postgrex.Types.define(MyApp.PostgrexTypes, [], json: Poison)
Run Code Online (Sandbox Code Playgroud)
然后在启动Postgrex时:
Postgrex.start_link(types: MyApp.PostgrexTypes)
Run Code Online (Sandbox Code Playgroud)
在Postgrex> = 0.14(目前是主人)上,它变得更容易了:
config :postgrex, :json_library, Poison
Run Code Online (Sandbox Code Playgroud)