Skip to content

Commit

Permalink
Fix MySQL multiple-table DELETE error (#14179)
Browse files Browse the repository at this point in the history
http://dev.mysql.com/doc/refman/5.7/en/delete.html
"You cannot use ORDER BY or LIMIT in a multiple-table DELETE."
  • Loading branch information
staudenmeir authored and taylorotwell committed Jul 1, 2016
1 parent 555269e commit d54500e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
12 changes: 6 additions & 6 deletions src/Illuminate/Database/Query/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,14 @@ public function compileDelete(Builder $query)
$sql = trim("delete $table from {$table}{$joins} $where");
} else {
$sql = trim("delete from $table $where");
}

if (isset($query->orders)) {
$sql .= ' '.$this->compileOrders($query, $query->orders);
}
if (isset($query->orders)) {
$sql .= ' '.$this->compileOrders($query, $query->orders);
}

if (isset($query->limit)) {
$sql .= ' '.$this->compileLimit($query, $query->limit);
if (isset($query->limit)) {
$sql .= ' '.$this->compileLimit($query, $query->limit);
}
}

return $sql;
Expand Down
9 changes: 7 additions & 2 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1094,18 +1094,23 @@ public function testDeleteMethod()
$builder->getConnection()->shouldReceive('delete')->once()->with('delete from "users" where "id" = ?', [1])->andReturn(1);
$result = $builder->from('users')->delete(1);
$this->assertEquals(1, $result);

$builder = $this->getMySqlBuilder();
$builder->getConnection()->shouldReceive('delete')->once()->with('delete from `users` where `email` = ? order by `id` asc limit 1', ['foo'])->andReturn(1);
$result = $builder->from('users')->where('email', '=', 'foo')->orderBy('id')->take(1)->delete();
$this->assertEquals(1, $result);
}

public function testDeleteWithJoinMethod()
{
$builder = $this->getMySqlBuilder();
$builder->getConnection()->shouldReceive('delete')->once()->with('delete `users` from `users` inner join `contacts` on `users`.`id` = `contacts`.`id` where `email` = ?', ['foo'])->andReturn(1);
$result = $builder->from('users')->join('contacts', 'users.id', '=', 'contacts.id')->where('email', '=', 'foo')->delete();
$result = $builder->from('users')->join('contacts', 'users.id', '=', 'contacts.id')->where('email', '=', 'foo')->orderBy('id')->limit(1)->delete();
$this->assertEquals(1, $result);

$builder = $this->getMySqlBuilder();
$builder->getConnection()->shouldReceive('delete')->once()->with('delete `users` from `users` inner join `contacts` on `users`.`id` = `contacts`.`id` where `id` = ?', [1])->andReturn(1);
$result = $builder->from('users')->join('contacts', 'users.id', '=', 'contacts.id')->delete(1);
$result = $builder->from('users')->join('contacts', 'users.id', '=', 'contacts.id')->orderBy('id')->take(1)->delete(1);
$this->assertEquals(1, $result);
}

Expand Down

0 comments on commit d54500e

Please sign in to comment.