Collect conflicts?


Currently I upload a CSV file which I parse then and use Repo.insert_all for all entries to insert into DB.
I would like to collect conflicts for the user, to give him a chance to fix his uploaded CSV file for the troublesome entries.

So, as for my question, I need to utilize on_conflict to collect all conflicts and return them to user, is this possible? any idea to do so?

1 Like

I am afraid you cannot do so using on_conflict. You will need to perform multiple queries inside a transaction.

1 Like

Hi @sabri …here’s a question I asked recently almost along the same lines as yours… Collect conflicts?. I wanted a list of the entries just inserted.I may be completely off the mark here but can you not do this

def itemsToInsert =[items to be inserted]

outcome = Repo.insert_all(Model,itemsToInsert,on_conflict: :nothing,returning: [:somecolumn])

def newList= [You then pattern match or something and get recently inserted items]

def conflictedItems= itemsToList – newList

Hope this helps.

1 Like

But, how can you get the recently inserted items? I guess using the date by using a query?
Why did you use returning: in Repo.insert_all ?
As for the link of your question, it’s the same link of my question :slight_smile:

1 Like

That would be handy if can be done in future, but is there any example about performing multiple queries inside a transaction?

1 Like

@sabri …sorry…here’s the link…I used returning to specify what column I want back after insert_all.

1 Like

Thanks, your thread helped me to get all the names of users who were inserted, as I used returning

1 Like

You’re welcome…I know these are not Oscar awards but the real credit goes to Jose for putting me in the right direction.