How to fix `take(p, ^only)` is not a valid query expression

Hello ,

I have 2 problems with Ecto‍‍ for loading all field. please see my following code :

only = [:status, :title, :inserted_at]
		query = from p in PostCategory, order_by: p.inserted_at, select: take(p, ^only)
		Repo.all(query)

my terminal :

== Compilation error in file lib/cms/post/post_query.ex ==
** (Ecto.Query.CompileError) `take(p, ^only)` is not a valid query expression.

* If you intended to call a database function, please check the documentation
  for Ecto.Query to see the supported database expressions

* If you intended to call an Elixir function or introduce a value,
  you need to explicitly interpolate it with ^

    (ecto) expanding macro: Ecto.Query.select/3
    (trangell_cms_service) lib/cms/post/post_query.ex:74: TrangellCmsService.Cms.Post.PostQuery.load_all_category_info/0
    (ecto) expanding macro: Ecto.Query.from/2
    (trangell_cms_service) lib/cms/post/post_query.ex:74: TrangellCmsService.Cms.Post.PostQuery.load_all_category_info/0
** (exit) shutdown: 1
    (mix) lib/mix/tasks/compile.ex:96: Mix.Tasks.Compile.run/1
    (mix) lib/mix/task.ex:314: Mix.Task.run_task/3
    (iex) lib/iex/helpers.ex:94: IEx.Helpers.recompile/0

or I had an error when I was traying to writte folowing code :

	query = from p in PostCategory, order_by: p.inserted_at, select: p.{status, title, inserted_at}
		Repo.all(query)

and its error :

== Compilation error in file lib/cms/post/post_query.ex ==
** (Ecto.Query.CompileError) `p.{status, title, inserted_at}` is not a valid query expression
    (ecto) expanding macro: Ecto.Query.select/3
    (trangell_cms_service) lib/cms/post/post_query.ex:76: TrangellCmsService.Cms.Post.PostQuery.load_all_category_info/0
    (ecto) expanding macro: Ecto.Query.from/2
    (trangell_cms_service) lib/cms/post/post_query.ex:76: TrangellCmsService.Cms.Post.PostQuery.load_all_category_info/0
** (exit) shutdown: 1
    (mix) lib/mix/tasks/compile.ex:96: Mix.Tasks.Compile.run/1
    (mix) lib/mix/task.ex:314: Mix.Task.run_task/3
    (iex) lib/iex/helpers.ex:94: IEx.Helpers.recompile/0

Please help me to fix these error and how to use they.

Thanks.

ref : https://github.com/elixir-ecto/ecto/issues/1145

ver my Ecto : 2.1
psql (PostgreSQL) 10.1
Elixir v1.6.4
mac os high sierra

1 Like

I don’t think there is a take function, try map instead.

only = [:status, :title, :inserted_at]
query = from p in PostCategory, order_by: p.inserted_at, select: map(p, ^only)
Repo.all(query)
2 Likes

Thank you , it works, I have a question, why doesn’t following code work for me ?

query = from p in PostCategory, order_by: p.inserted_at, select: p.{status, title, inserted_at}
		Repo.all(query)

I’m not very deep in ecto, but p.{status, title, inserted_at} looks plain wrong to me. Probably its {p.status, p.title, p.inserted_at} you want?

@NobbZ , please see following link
https://github.com/elixir-ecto/ecto/issues/1145

from p in Post, where: p.published, select: p.{name, title, published_at}

I wanted to remove additional p flag or I wanted to use dynamically in Ecto. otherwise I knew it .

That has been a proposal of three different alternatives, whereas option 2 (a function) was choosen from. The funtion in the proposal was named take/2, but it seems as if it is called map/2 in the actual implementation.

3 Likes