BryanJBryce

BryanJBryce

Ash default create action and keys

I have a question about the behavior of the default create action behavior.

I have a resource: Section, it has a uuid primary key and here is the relevant relationship declaration:

relationships do
    belongs_to :exam, App.MCAT.Exam, allow_nil?: false, writable?: true
    
    ...

  end

When I try to create a Section using the default create action like this:

Section.create!(%{exam_id: exam.id, type: :phys, order: 1, allotted_time_seconds: 60 * 95})

I get an Invalid Input error that says: “relationship exam is required”

I feel like including the id has worked for me before, but I think that is when the id was a primary key.

Is there a way to specify this field with the default create or do I need to make a custom create that accepts exam_id explicitly?

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

The relationship being writable?: true refers to the relationship being usable with manage_relationship, which they are by default. What you are looking for is attribute_writable?: true, which allows writing to the generated attribute that underpins the belongs_to relationship.

Also Liked

BryanJBryce

BryanJBryce

Doh :man_facepalming:. Thank you! And you’re welcome to anyone who runs into this same thing :winking_face_with_tongue:

Where Next?

Popular in Questions Top

New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
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
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New

Other popular topics Top

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
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
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
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
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
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

We're in Beta

About us Mission Statement