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

bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
Tee
can someone please explain to me how Enum.reduce works with maps
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
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New

We're in Beta

About us Mission Statement