Fl4m3Ph03n1x

Fl4m3Ph03n1x

How to swap which table is returned in Ecto?

Background

I have a a couple of queries that result in the archival of some metadata items. My objective is to merge these queries into one (if possible).

Code

meta_item_ids_to_archive =
      Post
      |> where([post], post.archive_meta)
      |> select([post], %{item_id: post.meta_id})
      |> subquery()

archive_meta_items_query =
      Meta
      |> join(:inner, [item], item_to_archive in ^meta_item_ids_to_archive,
        on: item.id == item_to_archive.item_id
      )

########
later on
########

Multi.update_all(
  :query_a, 
  archive_meta_items_query, 
  set: [archived: true]
)

So my original query starts with Post and then int eh subsequent query I start with Meta. I want to archive items in the Meta table. My first attempt at merging these queries was the following:


new_query = 
  Post
  |> where([post], post.archive_meta)
  |> join(:inner, [post], item_to_archive in Meta,
        on: post.meta_id == item_to_archive.item_id
  )

Multi.update_all(
  :query_a, 
  new_query, 
  set: [archived: true]
)

Problem

Now, the issue here is that this won’t work:

field archived in update does not exist in schema Post in query (…):

Because my new_query starts with Post instead of Meta, the update operation fails.

Question

  • Is there a way to tell Ecto “I want the Meta table returned instead of Post?”
  • If now, how can I solve this?

Marked As Solved

al2o3cr

al2o3cr

The first approach that comes to mind is to reverse the join - since it’s an INNER, either form will return the same rows:

new_query = 
  Meta
  |> join(:inner, [meta], post in Post,
        on: post.meta_id == meta.item_id.
        on: post.archive_meta
  )

Multi.update_all(
  :query_a, 
  new_query, 
  set: [archived: true]
)

Also Liked

LostKobrakai

LostKobrakai

I don’t think you need a subquery. A plain join might be enough. Though those questions indicate even more that it might be worthwhile investing into learning more about sql – this will in turn make it easier to create queries with ecto as well.

Looking at things just from the ecto perspective can be deceiving, as “code” is usually much more flexible than a declarative sql query.

Where Next?

Popular in Questions Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement