Skip to content

Commit a6db48e

Browse files
[10.x] Fix postgreSQL reserved word column names w/ guarded attributes 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
1 parent 87b9e79 commit a6db48e

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

src/Illuminate/Database/Query/Processors/PostgresProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function processColumns($results)
5959
$autoincrement = $result->default !== null && str_starts_with($result->default, 'nextval(');
6060

6161
return [
62-
'name' => $result->name,
62+
'name' => str_starts_with($result->name, '"') ? str_replace('"', '', $result->name) : $result->name,
6363
'type_name' => $result->type_name,
6464
'type' => $result->type,
6565
'collation' => $result->collation,

src/Illuminate/Database/Schema/Builder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,9 @@ public function whenTableDoesntHaveColumn(string $table, string $column, Closure
238238
*/
239239
public function getColumnType($table, $column, $fullDefinition = false)
240240
{
241-
$table = $this->connection->getTablePrefix().$table;
242-
243241
if (! $this->connection->usingNativeSchemaOperations()) {
242+
$table = $this->connection->getTablePrefix().$table;
243+
244244
return $this->connection->getDoctrineColumn($table, $column)->getType()->getName();
245245
}
246246

tests/Integration/Database/EloquentModelTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,38 @@ public function testDiscardChanges()
9494
$user->save();
9595
$this->assertFalse($user->wasChanged());
9696
}
97+
98+
public function testInsertRecordWithReservedWordFieldName()
99+
{
100+
Schema::create('actions', function (Blueprint $table) {
101+
$table->id();
102+
$table->string('label');
103+
$table->timestamp('start');
104+
$table->timestamp('end')->nullable();
105+
$table->boolean('analyze');
106+
});
107+
108+
$model = new class extends Model
109+
{
110+
protected $table = 'actions';
111+
protected $guarded = ['id'];
112+
public $timestamps = false;
113+
};
114+
115+
$model->newInstance()->create([
116+
'label' => 'test',
117+
'start' => '2023-01-01 00:00:00',
118+
'end' => '2024-01-01 00:00:00',
119+
'analyze' => true,
120+
]);
121+
122+
$this->assertDatabaseHas('actions', [
123+
'label' => 'test',
124+
'start' => '2023-01-01 00:00:00',
125+
'end' => '2024-01-01 00:00:00',
126+
'analyze' => true,
127+
]);
128+
}
97129
}
98130

99131
class TestModel1 extends Model

0 commit comments

Comments
 (0)