Skip to content

Commit afc7f1a

Browse files
authored
Merge pull request #2201 from MGatner/foreign-key-columns
Foreign key columns
2 parents 3881221 + 42b42ae commit afc7f1a

5 files changed

Lines changed: 27 additions & 10 deletions

File tree

system/Database/MySQLi/Connection.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -537,10 +537,14 @@ public function _foreignKeyData(string $table): array
537537
SELECT
538538
tc.CONSTRAINT_NAME,
539539
tc.TABLE_NAME,
540-
rc.REFERENCED_TABLE_NAME
540+
kcu.COLUMN_NAME,
541+
rc.REFERENCED_TABLE_NAME,
542+
kcu.REFERENCED_COLUMN_NAME
541543
FROM information_schema.TABLE_CONSTRAINTS AS tc
542544
INNER JOIN information_schema.REFERENTIAL_CONSTRAINTS AS rc
543545
ON tc.CONSTRAINT_NAME = rc.CONSTRAINT_NAME
546+
INNER JOIN information_schema.KEY_COLUMN_USAGE AS kcu
547+
ON tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME
544548
WHERE
545549
tc.CONSTRAINT_TYPE = ' . $this->escape('FOREIGN KEY') . ' AND
546550
tc.TABLE_SCHEMA = ' . $this->escape($this->database) . ' AND
@@ -555,10 +559,12 @@ public function _foreignKeyData(string $table): array
555559
$retVal = [];
556560
foreach ($query as $row)
557561
{
558-
$obj = new \stdClass();
559-
$obj->constraint_name = $row->CONSTRAINT_NAME;
560-
$obj->table_name = $row->TABLE_NAME;
561-
$obj->foreign_table_name = $row->REFERENCED_TABLE_NAME;
562+
$obj = new \stdClass();
563+
$obj->constraint_name = $row->CONSTRAINT_NAME;
564+
$obj->table_name = $row->TABLE_NAME;
565+
$obj->column_name = $row->COLUMN_NAME;
566+
$obj->foreign_table_name = $row->REFERENCED_TABLE_NAME;
567+
$obj->foreign_column_name = $row->REFERENCED_COLUMN_NAME;
562568

563569
$retVal[] = $obj;
564570
}

system/Database/Postgre/Connection.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -406,11 +406,13 @@ public function _foreignKeyData(string $table): array
406406
$retVal = [];
407407
foreach ($query as $row)
408408
{
409-
$obj = new \stdClass();
410-
$obj->constraint_name = $row->constraint_name;
411-
$obj->table_name = $row->table_name;
412-
$obj->foreign_table_name = $row->foreign_table_name;
413-
$retVal[] = $obj;
409+
$obj = new \stdClass();
410+
$obj->constraint_name = $row->constraint_name;
411+
$obj->table_name = $row->table_name;
412+
$obj->column_name = $row->column_name;
413+
$obj->foreign_table_name = $row->foreign_table_name;
414+
$obj->foreign_column_name = $row->foreign_column_name;
415+
$retVal[] = $obj;
414416
}
415417

416418
return $retVal;

system/Database/SQLite3/Connection.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ public function _foreignKeyData(string $table): array
407407
$obj->constraint_name = $row->from . ' to ' . $row->table . '.' . $row->to;
408408
$obj->table_name = $table;
409409
$obj->foreign_table_name = $row->table;
410+
$obj->sequence = $row->seq;
410411

411412
$retVal[] = $obj;
412413
}

tests/system/Database/Live/ForgeTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,13 @@ public function testForeignKey()
323323
if ($this->db->DBDriver === 'SQLite3')
324324
{
325325
$this->assertEquals($foreignKeyData[0]->constraint_name, 'users_id to db_forge_test_users.id');
326+
$this->assertEquals($foreignKeyData[0]->sequence, 0);
326327
}
327328
else
328329
{
329330
$this->assertEquals($foreignKeyData[0]->constraint_name, $this->db->DBPrefix . 'forge_test_invoices_users_id_foreign');
331+
$this->assertEquals($foreignKeyData[0]->column_name, 'users_id');
332+
$this->assertEquals($foreignKeyData[0]->foreign_column_name, 'id');
330333
}
331334
$this->assertEquals($foreignKeyData[0]->table_name, $this->db->DBPrefix . 'forge_test_invoices');
332335
$this->assertEquals($foreignKeyData[0]->foreign_table_name, $this->db->DBPrefix . 'forge_test_users');

user_guide_src/source/database/metadata.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ Usage example::
163163
{
164164
echo $key->constraint_name;
165165
echo $key->table_name;
166+
echo $key->column_name;
166167
echo $key->foreign_table_name;
168+
echo $key->foreign_column_name;
167169
}
168170

171+
The object fields may be unique to the database you are using. For instance, SQLite3 does
172+
not return data on column names, but has the additional *sequence* field for compound
173+
foreign key definitions.

0 commit comments

Comments
 (0)