用`embeds_many`查询

时间:2018-08-01 14:20:52

标签: postgresql elixir ecto

我正在使用具有embeds_many这样的代码:

defmodule People do
 (...)
  schema "people" do
    embeds_many :addresses, Address
  end
  (...)
end

defmodule Address do
  (...)
  @primary_key {:id, :binary_id, autogenerate: false}
  embedded_schema do
    field :type, :string
  end
  (...)
end

地址的典型值为:

addresses: [
    %Address{
      id: "123",
      type: "home"
    }
  ]

我如何查询所有具有id = 123和类型= home的地址的人?

编辑:根据建议,我已经尝试过:

query =
  from p in People,
  where: fragment(
    "? <@ ANY(?)",
    ~S|{"id": 123, "type":"home"}|,
    p.addresses
  )

Repo.all(query)

我收到此错误

** (Postgrex.Error) ERROR 42809 (wrong_object_type): op ANY/ALL (array) requires array on right side

编辑:此错误是由于迁移中字段的定义错误。 它被定义为map,根据embeds_many documentation,它应该是{:array, :map}

1 个答案:

答案 0 :(得分:0)

一个人可能会使用SELECT * FROM Order LEFT JOIN Sales ON Order.ID = Sales.ID LEFT JOIN Employee ON Order.tag = Employee.tag 和内部PostgreSQL功能:

fragment