On delete, set id to 1 - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: On delete, set id to 1 (/showthread.php?tid=74107) |
On delete, set id to 1 - Ajax30 - 07-23-2019 I am working on a basic blog application with Codeigniter 3.1.8 and Bootstrap 4. NOTE: My database does use InnoDB. There is a posts and a categories table. At the moment, the application inserts a category id into the posts table, whenever a post is created (you can not add a post before choosing a category for it), but I have set no foreign key relationship between the two tables (for ease of development) and no cascading. Now I want to set a special cascading relationship between the two tables: whenever a category is deleted, all posts in that category should have 1 in the cat_id column. I have created an installation process for the application: after creating a database and providing its credentials to the Code: application/config/database.php file, you can run the Code: Install controller which will create all the necessary tables: Code: class Install extends CI_Controller { The migrations I use to create the posts table are: Code: public function up(){ What shall I replace the last line with (it has illustrative purposes): Code: $this->db->query('ALTER TABLE `posts` ADD FOREIGN KEY(`cat_id`) REFERENCES 'categories'(`id`) ON DELETE SET cat_id to 1;'); in order to get the desired result? RE: On delete, set id to 1 - InsiteFX - 07-24-2019 The last part should be: Code: ON DELETE CASCADE I still would add the deleted_at field name for soft deletes, CI 4 has this field already CI 4: created_at updated_at deleted_at |