Skip to content

Commit

Permalink
[10.x] Fix postgreSQL reserved word column names w/ guarded attribute…
Browse files Browse the repository at this point in the history
…s broken in native column attributes implementation (#48877)

* unquote quoted names on postgresql

* add test

* fix mysql 5.7 error

* fix table prefixed twice

* revert removing 'end' keyword
  • Loading branch information
hafezdivandari authored Nov 1, 2023
1 parent 87b9e79 commit a6db48e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function processColumns($results)
$autoincrement = $result->default !== null && str_starts_with($result->default, 'nextval(');

return [
'name' => $result->name,
'name' => str_starts_with($result->name, '"') ? str_replace('"', '', $result->name) : $result->name,
'type_name' => $result->type_name,
'type' => $result->type,
'collation' => $result->collation,
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,9 @@ public function whenTableDoesntHaveColumn(string $table, string $column, Closure
*/
public function getColumnType($table, $column, $fullDefinition = false)
{
$table = $this->connection->getTablePrefix().$table;

if (! $this->connection->usingNativeSchemaOperations()) {
$table = $this->connection->getTablePrefix().$table;

return $this->connection->getDoctrineColumn($table, $column)->getType()->getName();
}

Expand Down
32 changes: 32 additions & 0 deletions tests/Integration/Database/EloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,38 @@ public function testDiscardChanges()
$user->save();
$this->assertFalse($user->wasChanged());
}

public function testInsertRecordWithReservedWordFieldName()
{
Schema::create('actions', function (Blueprint $table) {
$table->id();
$table->string('label');
$table->timestamp('start');
$table->timestamp('end')->nullable();
$table->boolean('analyze');
});

$model = new class extends Model
{
protected $table = 'actions';
protected $guarded = ['id'];
public $timestamps = false;
};

$model->newInstance()->create([
'label' => 'test',
'start' => '2023-01-01 00:00:00',
'end' => '2024-01-01 00:00:00',
'analyze' => true,
]);

$this->assertDatabaseHas('actions', [
'label' => 'test',
'start' => '2023-01-01 00:00:00',
'end' => '2024-01-01 00:00:00',
'analyze' => true,
]);
}
}

class TestModel1 extends Model
Expand Down

0 comments on commit a6db48e

Please sign in to comment.