Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query builder updateOrInsert fails with empty values #27890

Closed
Organizm238 opened this issue Mar 15, 2019 · 4 comments
Closed

Query builder updateOrInsert fails with empty values #27890

Organizm238 opened this issue Mar 15, 2019 · 4 comments

Comments

@Organizm238
Copy link
Contributor

  • Laravel Version: 5.6.20
  • PHP Version: 7.1
  • Database Driver & Version: mysql

Description:

I am trying to do the following: insert the row to database, if it doesn't exists. I can do it conditionally, but I want to do it in one step with some useful method. Seems like Builder@updateOrInsert() is what I need:

public function updateOrInsert(array $attributes, array $values = [])
   {
       if (! $this->where($attributes)->exists()) {
           return $this->insert(array_merge($attributes, $values));
       }

       return (bool) $this->take(1)->update($values);
   }

I can see, that is has default value for values attribute - empty array. So, seems logical, that it should work with its default value. But it is not. Instead you get invalid mysql syntax error. Sql looks like this:

SQL: update `users` set  where (`some_field` = 'some_value') limit 1

You can see empty set block there.

Steps To Reproduce:

  1. Run this:
$existingUserEmail = 'SOME_EXISTING_EMAIL';
DB::table('users')->updateOrInsert(['email' => $existingUserEmail]);
  1. Get this:
 SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where (`email`   
  = ?) limit 1' at line 1 (SQL: update `users` set  where (`email` = SOME_EXISTING_EMAIL) limit 1)
@staudenmeir
Copy link
Contributor

If $values is empty, an UPDATE query makes no sense, right? So we just return true?

@Organizm238
Copy link
Contributor Author

Organizm238 commented Mar 15, 2019

If $values is empty, an UPDATE query makes no sense, right? So we just return true?

Yes, it would be fine. There should be a check for empty $values, I suppose.

@staudenmeir
Copy link
Contributor

Can you submit a PR?

@driesvints
Copy link
Member

Heya, unfortunately we don't support this version anymore. Please check out our support policy on which versions we are currently supporting. Can you please try to upgrade to the latest version and see if your problem persists? If so feel free to reply and we'll try to have a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants