anatomic's avatar

What happens when updateOrCreate can't update or create?

Hi

I'm updating (or creating) lots of records from an API, but I would like a way of catching when records can't update or create so I can save them for future auditing in case of problems.

Is there a way to find out when this happens, please?

Thanks

0 likes
12 replies
tykus's avatar

In what circumstances does updateOrCreate fail; do you have an example of the Exception that occurs?

anatomic's avatar

@tykus I don't have an example I'm afraid. I'm just trying to cover my backside in the event that the data that can't be updated or created is saved so I can reverse engineer why in case of problems. I need to try and cover all eventualities.

tykus's avatar

@anatomic generally

try {
	Model::updateOrCreate([/* unique attributes  */], [/* other attributes */]);
} catch (\Exception $e) {
	\Log::error($e->getMessage, $allAttributes);
}

You might catch a QueryException if an attribute conflicts with a unique constraint on the database; but, it was not used to find an existing record.

1 like
anatomic's avatar

@tykus It would appear that if it doesn't need to insert or update it doesn't throw an exception so logs nothing, but thanks.

anatomic's avatar

@tykus no idea, I presumed that was what you were trying to say.

I’m just wondering if I can “easily” work out if it doesn’t update or create, in other words if the new record is simply discarded.

Thanks for your help, please waste no more of your time. It’s not a huge issue, just curiosity really

tykus's avatar
tykus
Best Answer
Level 104

@anatomic no, I was asking earlier In what circumstances does updateOrCreate fail; and not updating an existing record because has the same attributes is not a failure.

What you can do is check two things; (i) if the Model wasRecentlyCreated (it was just created) and (ii) if an existing record was found, then wasChanged will inform if it was changed. So, if both are false, this is effectively the "failure" scenario:

$model = Model::updateOrCreate([/* unique attributes  */], [/* other attributes */]);
if (! $model->wasRecentlyCreated && ! $model->wasChanged()) {
    \Log::info('Model was neither updated nor created', [
        'model_id' => $model->id, // the found model
        'attributes' => $attributes // the 'new' attributes
    ]);
}
1 like
anatomic's avatar

@tykus Sorry, I think it's just a misuse of words on my part. I've never said it fails (using the word catching unfortunately implied that!), it works exactly as I expect. I just wanted to know how/if I can find when it doesn't update or create (because its not unique) and record that.

Thanks for your help, I think your solution should do the trick. :)

jlrdw's avatar

Have you setup a try catch block?

1 like
anatomic's avatar

@jlrdw I'm about to work on this, but from what I can work out, not updating or creating doesn't throw an exception does it?

Sinnbeck's avatar

Consider switching up upsert if you need to create/update alot of records

1 like
anatomic's avatar

@Sinnbeck Thanks, I've considered this, but so far the data I receive from the API means I have to insert one record at a time, which is not a huge issue as it's set up as a job to run on the schedule.

Please or to participate in this conversation.