Skip to content

Commit

Permalink
Use an alias for the row on upsert.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulandroshchuk committed Apr 19, 2022
1 parent 0805622 commit 5f1213f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Query/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,11 @@ protected function compileUpdateColumns(Builder $query, array $values)
*/
public function compileUpsert(Builder $query, array $values, array $uniqueBy, array $update)
{
$sql = $this->compileInsert($query, $values).' on duplicate key update ';
$sql = $this->compileInsert($query, $values).' as laravel_upsert_key on duplicate key update ';

$columns = collect($update)->map(function ($value, $key) {
return is_numeric($key)
? $this->wrap($value).' = values('.$this->wrap($value).')'
? $this->wrap($value).' = '.$this->wrap('laravel_upsert_key.'.$value)
: $this->wrap($key).' = '.$this->parameter($value);
})->implode(', ');

Expand Down
6 changes: 3 additions & 3 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2491,7 +2491,7 @@ public function testUpdateMethod()
public function testUpsertMethod()
{
$builder = $this->getMySqlBuilder();
$builder->getConnection()->shouldReceive('affectingStatement')->once()->with('insert into `users` (`email`, `name`) values (?, ?), (?, ?) on duplicate key update `email` = values(`email`), `name` = values(`name`)', ['foo', 'bar', 'foo2', 'bar2'])->andReturn(2);
$builder->getConnection()->shouldReceive('affectingStatement')->once()->with('insert into `users` (`email`, `name`) values (?, ?), (?, ?) as laravel_upsert_key on duplicate key update `email` = `laravel_upsert_key`.`email`, `name` = `laravel_upsert_key`.`name`', ['foo', 'bar', 'foo2', 'bar2'])->andReturn(2);
$result = $builder->from('users')->upsert([['email' => 'foo', 'name' => 'bar'], ['name' => 'bar2', 'email' => 'foo2']], 'email');
$this->assertEquals(2, $result);

Expand All @@ -2514,7 +2514,7 @@ public function testUpsertMethod()
public function testUpsertMethodWithUpdateColumns()
{
$builder = $this->getMySqlBuilder();
$builder->getConnection()->shouldReceive('affectingStatement')->once()->with('insert into `users` (`email`, `name`) values (?, ?), (?, ?) on duplicate key update `name` = values(`name`)', ['foo', 'bar', 'foo2', 'bar2'])->andReturn(2);
$builder->getConnection()->shouldReceive('affectingStatement')->once()->with('insert into `users` (`email`, `name`) values (?, ?), (?, ?) as laravel_upsert_key on duplicate key update `name` = `laravel_upsert_key`.`name`', ['foo', 'bar', 'foo2', 'bar2'])->andReturn(2);
$result = $builder->from('users')->upsert([['email' => 'foo', 'name' => 'bar'], ['name' => 'bar2', 'email' => 'foo2']], 'email', ['name']);
$this->assertEquals(2, $result);

Expand Down Expand Up @@ -2926,7 +2926,7 @@ public function testPreservedAreAppliedByInsertUsing()
public function testPreservedAreAppliedByUpsert()
{
$builder = $this->getMySqlBuilder();
$builder->getConnection()->shouldReceive('affectingStatement')->once()->with('insert into `users` (`email`) values (?) on duplicate key update `email` = values(`email`)', ['foo']);
$builder->getConnection()->shouldReceive('affectingStatement')->once()->with('insert into `users` (`email`) values (?) as laravel_upsert_key on duplicate key update `email` = `laravel_upsert_key`.`email`', ['foo']);
$builder->beforeQuery(function ($builder) {
$builder->from('users');
});
Expand Down

0 comments on commit 5f1213f

Please sign in to comment.