Skip to content

Commit e09b644

Browse files
authored
Merge pull request #4696 from paulbalandan/nullable-field
Fix nullable type not showing in SQL string
2 parents de9f474 + dd8b670 commit e09b644

File tree

5 files changed

+37
-3
lines changed

5 files changed

+37
-3
lines changed

system/Database/Forge.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ class Forge
150150
* NULL value representation in CREATE/ALTER TABLE statements
151151
*
152152
* @var string
153+
*
154+
* @internal Used for marking nullable fields. Not covered by BC promise.
153155
*/
154156
protected $null = '';
155157

system/Database/MySQLi/Forge.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,10 @@ class Forge extends BaseForge
8888
* NULL value representation in CREATE/ALTER TABLE statements
8989
*
9090
* @var string
91+
*
92+
* @internal
9193
*/
92-
protected $_null = 'NULL';
94+
protected $null = 'NULL';
9395

9496
//--------------------------------------------------------------------
9597

system/Database/Postgre/Forge.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ class Forge extends BaseForge
5353
* NULL value representation in CREATE/ALTER TABLE statements
5454
*
5555
* @var string
56+
*
57+
* @internal
5658
*/
57-
protected $_null = 'NULL';
59+
protected $null = 'NULL';
5860

5961
//--------------------------------------------------------------------
6062

system/Database/SQLite3/Forge.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ class Forge extends BaseForge
3131
* NULL value representation in CREATE/ALTER TABLE statements
3232
*
3333
* @var string
34+
*
35+
* @internal
3436
*/
35-
protected $_null = 'NULL';
37+
protected $null = 'NULL';
3638

3739
//--------------------------------------------------------------------
3840

tests/system/Database/Live/ForgeTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,32 @@ public function testCreateTableWithArrayFieldConstraints()
243243
}
244244
}
245245

246+
/**
247+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/4693
248+
*/
249+
public function testCreateTableWithNullableFieldsGivesNullDataType(): void
250+
{
251+
$this->forge->addField([
252+
'id' => ['type' => 'INT', 'constraint' => 11, 'auto_increment' => true],
253+
'name' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
254+
]);
255+
256+
$createTable = $this->getPrivateMethodInvoker($this->forge, '_createTable');
257+
258+
$sql = $createTable('forge_nullable_table', false, []);
259+
260+
if ($this->db->DBDriver !== 'SQLSRV')
261+
{
262+
// @see https://regex101.com/r/bIHVNw/1
263+
$this->assertMatchesRegularExpression('/(?:`name`|"name") VARCHAR(.*) NULL/', $sql);
264+
}
265+
else
266+
{
267+
// sqlsrv table fields are default nullable
268+
$this->assertMatchesRegularExpression('/"name" VARCHAR/', $sql);
269+
}
270+
}
271+
246272
public function testCreateTableWithStringField()
247273
{
248274
$this->forge->dropTable('forge_test_table', true);

0 commit comments

Comments
 (0)