sheerlox

sheerlox

Upsert not working on bulk_insert if the upsert_identity attribute is not accepted by the action

I was trying to upsert rows from a database export that I processed with Python and exported back to CSV to be loaded back from Elixir + Ash.

Instead of fetching all rows, updating the resources in memory, and using a bulk_update (not sure about that as I’ve never tried to use it for now. EDIT: welp, this is definitely not how bulk_update works :smiling_face_with_tear:), I figured it would be easier to just use bulk_insert and use my id attribute as the upsert_identity:

%{records: inserted_skills, error_count: skills_error_count, errors: skills_errors} =
  Ash.bulk_create(unique_skills_data, Skill, :create,
    return_records?: true,
    upsert?: true,
    upsert_identity: :id,
    upsert_fields: [:slug, :name, :is_product, :acronym, :acronym_of],
    return_errors?: true,
    select: [:id, :slug, :name]
    # skip_unknown_inputs: :*
  )

As you can see, I initially tried using skip_unknown_inputs because I was getting an UnknownInput error on the id attribute. Since my id isn’t writable and should not be accepted by actions, I didn’t see another workaround that wouldn’t risk data consistency.

After that, I couldn’t understand why the upsert wasn’t working (it just inserted new rows with new IDs), and I thought my code was at fault (whether Python notebook exporting the CSV or the Elixir import script).

It turns out that if I just make my id attribute accepted by the create action (and writable, as it’s a requirement to make it accepted by actions), the issue doesn’t arise anymore.

I’m not sure whether you’d consider that a bug or a feature. On my side, I’d consider it somewhat of a bug, especially since I’m not trying to write to it (it’s not in my upsert_fields list, which then would make sense to error).

I think for now I’ll consider updating in memory and using bulk_update EDIT: [make my id attribute writable & accepted] (unless there’s a better way I’m not aware of), but figured out it would be interesting to raise a discussion on the topic!

All the best :grin:

Most Liked

zachdaniel

zachdaniel

Creator of Ash

So, the reason it has to be writable is because, if it actually does turn out to be a create, you will be creating a record with the value provided. There isn’t really any way to get around that.

zachdaniel

zachdaniel

Creator of Ash

Doing well, thanks! Busy busy busy as always :laughing:

Where Next?

Popular in Discussions Top

Donovan
Hello everyone, I’m so glad to have discovered this awesome community. Thanks for creating it! This is my second post, and apologies for...
New
owaisqayum
I have a sample string sentence = "Hello, world ... 123 *** ^%&*())^% %%:>" From this string, I want to only keep the integers, ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
WolfDan
After doing a port from a c++ library to my project in phoenix I’ve seen that I need a faster way to run this algorithm and I found this ...
New
ricklove
I was just introduced to Elixir and Phoenix. I was told about the 2 million websocket test that was done 2 years ago. From my research, t...
New
nunobernardes99
Hi there Elixir friends :vulcan_salute: In a recent task I was on, I needed to check in two dates which of them is the maximum and which...
New
AlexMcConnell
The reason that Rails is as popular as it is is because it’s very easy for relatively inexperienced developers to get a lot of work done....
588 19652 166
New
CharlesO
Erlang :list.nth simple, but 1 - based nth(1, [H|_]) -> H; nth(N, [_|T]) when N > 1 -> nth(N - 1, T). Elixir Enum.at … coo...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
AstonJ
It’s been a while since we’ve had a thread like this, so what better way to kick off the year with :003: What does being an Elixir user ...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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
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
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
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
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
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

Elixir Forum

We're in Beta

About us Mission Statement