Part of the EllisLab Network
   
 
How to add FOREIGN KEY constraint when creating a table?
Posted: 10 February 2012 02:59 AM   [ Ignore ]  
Summer Student
Total Posts:  22
Joined  01-19-2011

I am trying to create a table which has a foreign key reference to another table. The table is being created by CodeIgnier. However, in

$this->dbforge->add_key() 

I cannot see how would I indicate that this key should be a foreign key which references another key. How would I do that?

Profile
 
 
Posted: 11 February 2012 01:50 AM   [ Ignore ]   [ # 1 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  291
Joined  03-18-2011
$this->dbforge->add_key('KEY_ID'TRUE); 
Profile
 
 
Posted: 11 February 2012 03:32 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  22
Joined  01-19-2011
Bhashkar - 11 February 2012 01:50 AM
$this->dbforge->add_key('KEY_ID'TRUE); 

That’s the private key, not foreign key.

Profile
 
 
Posted: 11 February 2012 05:24 AM   [ Ignore ]   [ # 3 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  5366
Joined  06-19-2009

CodeIgniter User Guide - Custom Function Calls

I think it can also be done using a query I’ll see if I can get an example for you.

Update:

Examples:

// Create a database table.
$this->db->query('
CREATE TABLE IF NOT EXISTS `role` (
  `id`          int(11)      UNSIGNED NOT NULL AUTO_INCREMENT,
  `name`        varchar(255)          NOT NULL UNIQUE,
  `description` text                           DEFAULT NULL,
  PRIMARY KEY(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ;'
);

// These will work also if your table is already created!

// Example: But not needed here
// $this->db->protect_identifiers('role');

// First we add our column that will be the Foreign Key.
$this->db->query('ALTER TABLE `role` ADD COLUMN `parent` int(11) UNSIGNED DEFAULT NULL;');

// Next we add an index. Why? because mysql says so and FKs rely on indexes
$this->db->query('ALTER TABLE `role` ADD INDEX `parent` (`parent`);');

// Now we add our constraint.
$this->db->query('ALTER TABLE `role` ADD CONSTRAINT `parent` FOREIGN KEY(`parent`) REFERENCES `role`(`id`) ON DELETE SET NULL ON UPDATE SET NULL;');

// Add foreign key
$this->db->query('ALTER TABLE `role` ADD FOREIGN KEY(`id`) REFERENCES settings(`id`) ON DELETE CASCADE ON UPDATE CASCADE;'); 

All have been tested and working here!

You could also make this into a method and pass the parameters into it…

 

 Signature 

Custom Designed Icons, eBook Covers Software Boxes. CD, DVD Etc. New iPhone® Tab Bar Icons and iPhone® Applications Icons.

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

Input -> Controller | Processing -> Model | Output -> View

Profile
 
 
Posted: 11 February 2012 07:46 PM   [ Ignore ]   [ # 4 ]  
Summer Student
Total Posts:  22
Joined  01-19-2011

Thank you, InsiteFX! That’s a great example!

I understand that anything allowed by the DBMS can be executed through an SQL query directly in CI. What I was looking for is the CI implementation of Foreign keys using functions just like add_key().

The idea I was looking if FK was implemented is that I wanted to commit FK implementation together with an updated CUBRID Database Driver. If you happen to know why it hasn’t been implemented so far, please let me know. I just keep thinking that maybe there is a reason why CI doesn’t have FK support.

Profile