Skip to content

Commit 4a949b3

Browse files
committed
fix: primary_key value
1 parent c86ddc5 commit 4a949b3

2 files changed

Lines changed: 67 additions & 5 deletions

File tree

system/Database/SQLite3/Connection.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,14 @@ protected function _fieldData(string $table): array
269269
for ($i = 0, $c = count($query); $i < $c; $i++) {
270270
$retVal[$i] = new stdClass();
271271

272-
$retVal[$i]->name = $query[$i]->name;
273-
$retVal[$i]->type = $query[$i]->type;
274-
$retVal[$i]->max_length = null;
275-
$retVal[$i]->default = $query[$i]->dflt_value;
276-
$retVal[$i]->primary_key = $query[$i]->pk ?? false;
272+
$retVal[$i]->name = $query[$i]->name;
273+
$retVal[$i]->type = $query[$i]->type;
274+
$retVal[$i]->max_length = null;
275+
$retVal[$i]->default = $query[$i]->dflt_value;
276+
// "pk" (either zero for columns that are not part of the primary key,
277+
// or the 1-based index of the column within the primary key).
278+
// https://www.sqlite.org/pragma.html#pragma_table_info
279+
$retVal[$i]->primary_key = ($query[$i]->pk === 0) ? 0 : 1;
277280
$retVal[$i]->nullable = isset($query[$i]->notnull) && ! (bool) $query[$i]->notnull;
278281
}
279282

tests/system/Database/Live/SQLite3/GetFieldDataTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,63 @@ public function testGetFieldData(): void
102102
json_encode($fields)
103103
);
104104
}
105+
106+
protected function createTableCompositePrimaryKey()
107+
{
108+
$this->forge->dropTable('test1', true);
109+
110+
$this->forge->addField([
111+
'pk1' => [
112+
'type' => 'VARCHAR',
113+
'constraint' => 64,
114+
],
115+
'pk2' => [
116+
'type' => 'VARCHAR',
117+
'constraint' => 64,
118+
],
119+
'text' => [
120+
'type' => 'VARCHAR',
121+
'constraint' => 64,
122+
],
123+
]);
124+
$this->forge->addPrimaryKey(['pk1', 'pk2']);
125+
$this->forge->createTable('test1');
126+
}
127+
128+
public function testGetFieldDataCompositePrimaryKey(): void
129+
{
130+
$this->createTableCompositePrimaryKey();
131+
132+
$fields = $this->db->getFieldData('test1');
133+
134+
$this->assertJsonStringEqualsJsonString(
135+
json_encode([
136+
(object) [
137+
'name' => 'pk1',
138+
'type' => 'VARCHAR',
139+
'max_length' => null,
140+
'default' => null,
141+
'primary_key' => 1,
142+
'nullable' => false,
143+
],
144+
(object) [
145+
'name' => 'pk2',
146+
'type' => 'VARCHAR',
147+
'max_length' => null,
148+
'default' => null,
149+
'primary_key' => 1,
150+
'nullable' => false,
151+
],
152+
(object) [
153+
'name' => 'text',
154+
'type' => 'VARCHAR',
155+
'max_length' => null,
156+
'default' => null,
157+
'primary_key' => 0,
158+
'nullable' => false,
159+
],
160+
]),
161+
json_encode($fields)
162+
);
163+
}
105164
}

0 commit comments

Comments
 (0)