Skip to content

Commit

Permalink
Merge pull request #2201 from MGatner/foreign-key-columns
Browse files Browse the repository at this point in the history
Foreign key columns
  • Loading branch information
lonnieezell authored Sep 9, 2019
2 parents 3881221 + 42b42ae commit afc7f1a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
16 changes: 11 additions & 5 deletions system/Database/MySQLi/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,10 +537,14 @@ public function _foreignKeyData(string $table): array
SELECT
tc.CONSTRAINT_NAME,
tc.TABLE_NAME,
rc.REFERENCED_TABLE_NAME
kcu.COLUMN_NAME,
rc.REFERENCED_TABLE_NAME,
kcu.REFERENCED_COLUMN_NAME
FROM information_schema.TABLE_CONSTRAINTS AS tc
INNER JOIN information_schema.REFERENTIAL_CONSTRAINTS AS rc
ON tc.CONSTRAINT_NAME = rc.CONSTRAINT_NAME
INNER JOIN information_schema.KEY_COLUMN_USAGE AS kcu
ON tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME
WHERE
tc.CONSTRAINT_TYPE = ' . $this->escape('FOREIGN KEY') . ' AND
tc.TABLE_SCHEMA = ' . $this->escape($this->database) . ' AND
Expand All @@ -555,10 +559,12 @@ public function _foreignKeyData(string $table): array
$retVal = [];
foreach ($query as $row)
{
$obj = new \stdClass();
$obj->constraint_name = $row->CONSTRAINT_NAME;
$obj->table_name = $row->TABLE_NAME;
$obj->foreign_table_name = $row->REFERENCED_TABLE_NAME;
$obj = new \stdClass();
$obj->constraint_name = $row->CONSTRAINT_NAME;
$obj->table_name = $row->TABLE_NAME;
$obj->column_name = $row->COLUMN_NAME;
$obj->foreign_table_name = $row->REFERENCED_TABLE_NAME;
$obj->foreign_column_name = $row->REFERENCED_COLUMN_NAME;

$retVal[] = $obj;
}
Expand Down
12 changes: 7 additions & 5 deletions system/Database/Postgre/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,13 @@ public function _foreignKeyData(string $table): array
$retVal = [];
foreach ($query as $row)
{
$obj = new \stdClass();
$obj->constraint_name = $row->constraint_name;
$obj->table_name = $row->table_name;
$obj->foreign_table_name = $row->foreign_table_name;
$retVal[] = $obj;
$obj = new \stdClass();
$obj->constraint_name = $row->constraint_name;
$obj->table_name = $row->table_name;
$obj->column_name = $row->column_name;
$obj->foreign_table_name = $row->foreign_table_name;
$obj->foreign_column_name = $row->foreign_column_name;
$retVal[] = $obj;
}

return $retVal;
Expand Down
1 change: 1 addition & 0 deletions system/Database/SQLite3/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ public function _foreignKeyData(string $table): array
$obj->constraint_name = $row->from . ' to ' . $row->table . '.' . $row->to;
$obj->table_name = $table;
$obj->foreign_table_name = $row->table;
$obj->sequence = $row->seq;

$retVal[] = $obj;
}
Expand Down
3 changes: 3 additions & 0 deletions tests/system/Database/Live/ForgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,13 @@ public function testForeignKey()
if ($this->db->DBDriver === 'SQLite3')
{
$this->assertEquals($foreignKeyData[0]->constraint_name, 'users_id to db_forge_test_users.id');
$this->assertEquals($foreignKeyData[0]->sequence, 0);
}
else
{
$this->assertEquals($foreignKeyData[0]->constraint_name, $this->db->DBPrefix . 'forge_test_invoices_users_id_foreign');
$this->assertEquals($foreignKeyData[0]->column_name, 'users_id');
$this->assertEquals($foreignKeyData[0]->foreign_column_name, 'id');
}
$this->assertEquals($foreignKeyData[0]->table_name, $this->db->DBPrefix . 'forge_test_invoices');
$this->assertEquals($foreignKeyData[0]->foreign_table_name, $this->db->DBPrefix . 'forge_test_users');
Expand Down
5 changes: 5 additions & 0 deletions user_guide_src/source/database/metadata.rst
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ Usage example::
{
echo $key->constraint_name;
echo $key->table_name;
echo $key->column_name;
echo $key->foreign_table_name;
echo $key->foreign_column_name;
}

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

0 comments on commit afc7f1a

Please sign in to comment.