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…
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.