framework
framework copied to clipboard
updateOrCreate makes partial match
- Laravel Version: 9.19
- PHP Version: 8.1
- Database Driver & Version: MariaDB 10
Trying to update or create a new record, A record that has as the matching value: 102577 returns a match of 102577-01 and updates that instead of creating a new record. Example code:
foreach($this->newAccessories as $accessory)
{
Log::debug($accessory['artnr']);
Log::debug(Accessory::updateOrCreate(['artnr' => $accessory['artnr']], [
'title' => $accessory['title'],
'sec_title' => $accessory['sec_title'],
'name' => $accessory['name'],
'supartnr' => $accessory['supplier_artnr']
]));
}
Output:
[2022-08-10 16:47:51] local.DEBUG: 102577
[2022-08-10 16:47:51] local.DEBUG: {"id":1769,"artnr":"102577-01",...
What results do you get for:
Accessory::where(['artnr' => 102577])->first();
Also: what's the column type of artnr?
Tbh, I think something is going on specifically with your app. The query statement I added above is the exact one for updateOrCreate behind the scenes.
Can you first please try one of the support channels below? If you can actually identify this as a bug, feel free to open up a new issue with a link to the original one and we'll gladly help you out.
Thanks!
@driesvints Hello,
The query returns the same wrong result:
[2022-08-11 09:39:51] local.DEBUG: {"id":2055,"artnr":"102577-01",...
The type is VARCHAR UNIQUE and NULL is allowed.
I believe something is wrong with either the table itself or the configuration in Laravel. I will join Discord and discuss it further there. Thank you
@driesvints Yeah the problem was indeed the variable type. I got to cast the artnr to string and got the expected results:
Accessory::updateOrCreate(['artnr' => (string)$accessory['artnr']], [
'title' => $accessory['title'],
'sec_title' => $accessory['sec_title'],
'name' => $accessory['name'],
'supartnr' => $accessory['supplier_artnr']
])