fchoquet

fchoquet

Persist many to many association with additional columns

I’ve chosen Elixir/Phoenix to quickly prototype an idea. This is my first project with Phoenix and I’m struggling a little with things that should be obvious.
I haven’t accomplished anything yet but I’ve now consumed twice the time it would have taken me to complete the entire project in another language. Phoenix itself is pretty straightforward, but Ecto keeps standing on my way. I need some help before I give up and start over the project in another technology…

What I’m trying to achieve is pretty simple. I just want to attach categories to entities.
So for instance in a Requisition form there are several multi-select fields with different categories. There is one unique categories table with a type field.

The requisitions_category join table have an additional “context” column. Because the same category can be linked to the same req in different contexts (in that case that would be 2 different form fields) The forms are customisable in this app, that’s why I need something generic.

Because of this extra “context” field my understanding is that I can’t declare a many_to_many assoc.

Here are the (simplified) corresponding entities:

schema "requisitions_categories" do
    field :context, :string
    belongs_to :requisition, Requisition
    belongs_to :category, Category
 end
schema "requisitions" do
    field :title, :string
    has_many :contract_type, RequisitionCategory, where: [context: "contract_type"]
end
schema "categories" do
    field :title, :string
    field :type, :integer
end

Now I’m trying to persist a new requisition. So basically these steps:

  • Insert the requisition
  • Get the inserted ID
  • create the corresponding requisitions_categories from the inserted requisition ID, the hardcoded (for now) context and the category ID.

The problem is that whatever I try, either the categories are ignored, or I get cryptic error messages.

First thing I don’t manage to understand is if Ecto is supposed to handle this case of if I have to do everything manually. I assume it does (otherwise that’s the point or using an ORM?) and my understanding is that put_assoc should handle it, but it doesn’t work.

Or am I supposed to use Ecto.Multi and proceed step by step?

I haven’t found this case in any documentation, and googling it did not help. So any good article / doc would help.

Also, I’m generally speaking not a big fan of ORMs (feel free to ignore the “O”) so my last option would be to skip Ecto entirely and write good old SQL queries and keep full control other how I access my data. What library should I use for that? Is it even an option with Phoenix?

First Post!

LostKobrakai

LostKobrakai

Can you show some code and/or the errors you get. Even with your quite elaborate description I’m not really sure I understand the problem enough to be able to suggest something.

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
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
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
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
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
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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
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 47930 226
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

Latest on Elixir Forum

We're in Beta

About us Mission Statement