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 : Dynamically select/restrict fields in a query · Issue #1145 · elixir-ecto/ecto · GitHub
ver my Ecto : 2.1
psql (PostgreSQL) 10.1
Elixir v1.6.4
mac os high sierra