Troubles with many_to_many associations and Phoenix Web Rendering

Hi everyone,

I am new to Elixir and Phoenix , and I have hit a wall. I have troubles creating, with one Web page, 3 entries in our database. Here’s what’s happenning:

We have three tables:

Events <-> Event_Actions <-> Actions

Events and Actions are linked through a many_to_many association called Event_Action . Events actually have 3 many_to_many associations with Actions , through three different tables with different fields, but for the sake of the example I’ve simplified it to one.

When using the Phoenix Web interface, there is an error upon submitting the form.

The Event and Action entries are correctly created but an exception is raised for the association. For some reason, the Timestamps are not inserted.

If I look into the queries outputs, I can see the INSERT INTO actions or events have the timestamps fields but not the “event_actions”.

The error I get is this: ERROR 23502 (not_null_ciolation) null value in column “inserted_at” violates not-null constraint (table: event_start_actions)

If I run the function from iex or test it, it works, the problem only happens when it is executed from the web display.
Here’s the function we can test, and that works:

def create_action_on_start(event_id, action_id) do
    payload = %Event_Start_Action{
      event_id: event_id,
      action_id: action_id
    }
    Repo.insert(payload)
  end

If you need any other information, tell me and I’ll tell you. Me and my colleague has put this question on the Reddit and Stack-overflow, we just want this question to be seen.

Hello and welcome to the forum,

Usually, join table don’t have other fields than the keys… unless You want to keep some data, for example, Membership and joined_since etc.

In your case, You have timestamp on the join table…

Either remove timestamp, or create intermediate schema (Like membership would be)

You could show your migration file for events_actions table, to confirm that.

Hello and thanks for your reply!

I just got rid of the timestamps and it works. I thought I needed them because every example I found had them, and I didn’t want to get rid of something important. Thanks a lot, you got me out out of a pickle!

While I’m here, do you have any good practice recommendation concerning associations? So that I don’t make simple mistakes like this anymore!

1 Like