WammKD

WammKD

Inserting to Tables with Foreign Key Mappings (strategy suggestions)

So I have this tables setup where I’m receiving foreign data and need to insert it into my database.

These elements I receive can have a URL attached to them but may not. So I have a urls table which has foreign keys to a url_domains table and url_paths table. There is, of course, also the table for storing the elements received from the third party called elements.

To tie these both together, there’s a mapping table with a bigserial primary key and two columns with foreign keys to urls and elements; this addresses the fact that I may only have been given a URL or just an element with no URL or have an element which has a URL; all three cases can still get saved and indexed. For simplicity, let’s call this table mappings.

This is more for my edification of how to use Ecto so let’s assume that this setup works, is sane, and is the right way to go.

My question is…what is the best means of inserting data into this? There can be cases where the URL was received before the element so, in those cases, I need to update the row in mappings that has the URL of the element I just received to also now point to the right row in elements.

I can, obviously, insert each thing individually and manually check whether it already exists and perform updates to mappings when I find that something already exists and need to link it to the new element but this feels both verbose and inefficient.

Ideally, I’d like to receive the data, be able to formulate it into a single call that inserts the element, URL, or both (depending on what was received) with a new row in mappings that has proper references and, in cases where the element’s URL is already in mappings for the element I’ve just received, have things be smart enough to insert the element data into elements and then update the mappings row with the corresponding URL to our new elements row.

Is this feasible? Or is the complexity of the situation necessitate just manually handling each element and checking if any already exist and doing the updates myself? I know Ecto can do Upserts so, theoretically, this could be cleanly done but I’m having a hard time thinking about how that code might look.

Where Next?

Popular in Questions Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
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 Postgres...
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
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
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
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
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
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
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New

We're in Beta

About us Mission Statement