framework icon indicating copy to clipboard operation
framework copied to clipboard

updateOrCreate makes partial match

Open m33ts4k0z opened this issue 3 years ago • 0 comments

  • 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",...

m33ts4k0z avatar Aug 10 '22 16:08 m33ts4k0z

What results do you get for:

Accessory::where(['artnr' => 102577])->first();

driesvints avatar Aug 11 '22 07:08 driesvints

Also: what's the column type of artnr?

driesvints avatar Aug 11 '22 07:08 driesvints

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 avatar Aug 11 '22 07:08 driesvints

@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

m33ts4k0z avatar Aug 11 '22 07:08 m33ts4k0z

@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']                
  ])

m33ts4k0z avatar Aug 11 '22 07:08 m33ts4k0z