Ecto select fields with map function as alias or different name

iex(18)> fields = [:id]                                                                  
[:id]

iex(19)> fields_map = %{id: :job_id}                                                     
%{id: :job_id}

iex(20)> query = from p in Qber.V1.JobModel, where: p.id == 1, select: map(p, ^fields)
#Ecto.Query<from j in Qber.V1.JobModel, where: j.id == 1, select: map(j, [:id])>

I want to select my fields dynamically with AS names on the basis of params I got(which may have some joins as this app is going to have heavy admin dashboard queries). So I need dynamic solution. is there a way I can pass a map instead of list dynamically to map function and ecto will send me the selected result with custom names I passed in map.

Instead of

[%{id: 1}]

I want the result to be

[%{job_id: 1}]

dynamically.

Some more context
My question is related to this question actually
select-fields-dynamically and I have also posted a question here. As I am joining many tables with same keys and I either want these keys to be alias so that these can be unique or map them inside their table name for uniqueness. Thanks

You can’t select and rename dynamically. If the issue is conflict because of joins, one option is to nest those using select merge:

query = from p in Qber.V1.JobModel, where: p.id == 1, select_merge: %{job: map(p, ^fields)}

This way you don’t need renaming and you still get the dynamic selection.

I can’t even dynamically nest using select merge I think. if I write query like this

table_name = "dynamic"

query = from p in Qber.V1.JobModel, where: p.id == 1, select_merge: %{^table_name => map(p, ^fields)}

Ecto will automatically try to select $1 I dont know why(if Ecto doesn’t add $1 that would also solve the problem). I have many tables actually which require this so something dynamic would really solve this.

Any workaround will be really appreciated

Where what? o.O

if I run the above query

#Ecto.Query<from j in Qber.V1.JobModel, where: j.id == 1,
select: merge(j, %{^"dynamic" => j})>

, I will get

Repo.all query
[debug] QUERY ERROR source="jobs" db=8.2ms
SELECT j0."id", j0."service_id", j0."location_dest_zone_id", j0."cost", j0."initial_cost", j0."cost_at_heading", j0."cost_at_working", j0."cost_at_complete", j0."expected_work_duration", j0."work_duration_at_heading", j0."work_duration_at_working", j0."arrive_at", j0."confirmed_at", j0."started_heading_at", j0."started_working_at", j0."completed_at", j0."customer_rating", j0."provider_rating", j0."status_id", j0."basic_fee", j0."item_fee", j0."location_src", j0."location_dest", j0."location_src_zone_id", j0."service_params", j0."customer_comments", j0."cancel_reason", j0."provider_rating_friendly", j0."provider_rating_professional", j0."provider_rating_communication", j0."provider_comments", j0."location_src_name", j0."rejected_at", j0."cancelled_by", j0."cancelled_at", j0."wait_started_at", j0."updated_by", j0."approved_at", j0."called_at", j0."approved_by", j0."providers_request_id", j0."inserted_by", j0."title", j0."description", j0."deleted_by", j0."deleted_at", j0."inserted_at", j0."updated_at", $1 FROM "jobs" AS j0 WHERE (j0."id" = 1) AND (j0."deleted_at" IS NULL) ["dynamic"]
** (Postgrex.Error) ERROR 42P18 (indeterminate_datatype): could not determine data type of parameter $1
    (ecto) lib/ecto/adapters/sql.ex:431: Ecto.Adapters.SQL.execute_and_cache/7
    (ecto) lib/ecto/repo/queryable.ex:133: Ecto.Repo.Queryable.execute/5
    (ecto) lib/ecto/repo/queryable.ex:37: Ecto.Repo.Queryable.all/4