Skip to content

Select columns when query #10

Open

Description

@developerworks
def get_user_authorizations_order_by(user_id, limit \\ 5) do
  q = from(
    auth in __MODULE__,
    order_by: [asc: auth.date_created],
    where: auth.user_id == ^user_id,
    select: %{date_created: auth.date_created, ip: auth.ip},
    limit: ^limit
  )
  Logger.debug "q = #{inspect q}"
  q |> Repo.all
end

When i run it in IEx, error riased:

iex(9)> Authorization.get_user_authorizations_order_by 796486
2016-04-27 16:32:46.601 [debug] q = #Ecto.Query<from a in TelegramRiak.Model.Authorization, where: a.user_id == ^796486, order_by: [asc: a.date_created], limit: ^5, select: %{date_created: a.date_created, ip: a.ip}>
** (Ecto.QueryError) 1) Invalid expression for Riak adapter in select clause in query:

from a in TelegramRiak.Model.Authorization,
  where: a.user_id == ^...,
  order_by: [asc: a.date_created],
  limit: ^...,
  select: %{date_created: a.date_created, ip: a.ip}

    (riak_ecto) lib/riak_ecto/normalized_query.ex:383: Riak.Ecto.NormalizedQuery.error/2
    (riak_ecto) lib/riak_ecto/normalized_query.ex:161: Riak.Ecto.NormalizedQuery.projection/6
    (riak_ecto) lib/riak_ecto/normalized_query.ex:52: Riak.Ecto.NormalizedQuery.all/2
    (riak_ecto) lib/riak_ecto.ex:146: Riak.Ecto.execute/6
         (ecto) lib/ecto/repo/queryable.ex:95: Ecto.Repo.Queryable.execute/5
         (ecto) lib/ecto/repo/queryable.ex:15: Ecto.Repo.Queryable.all/4

Activity

  1. developerworks commented on Apr 27, 2016

    @developerworks
    Author

    I guess that riak is unstructured and object(map data type) as a whole to save, it's difficult to select individual columns from query, right?

  2. pma commented on Apr 27, 2016

    @pma
    Owner

    @developerworks It's certainly possible to implement this in the adapter.

    When we're querying by anything other than the ID, we're using the Solr interface instead of the KV interface. And Solr does support specifying the fields we want to retrieve. This is supported in riak_pb_socket.search/6 search option fl (field list):

    When querying via the KV interface, we always get the complete map from the database. But we can still respect the select to filter the fields to consider when building the Ecto.Schema struct.

  3. developerworks commented on Apr 28, 2016

    @developerworks
    Author
    And Solr does support specifying the fields we want to retrieve
    

    You means, if i want to specifying the fields we want to retrieve, i must use the :riakc_pb_socket.search/6 functions ?

  4. pma commented on May 1, 2016

    @pma
    Owner

    @developerworks The call to is done here: https://github.com/pma/riak_ecto/blob/master/lib/riak/connection.ex#L74

    To support specifying a subset of fields to be retrieved, we would have to properly handle Ecto.Query in Riak.Ecto.NormalizedQuery.all/2 and make sure it's passed down to the point where we pull the data from Riak.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions