How to add column with foreign key within migration? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: How to add column with foreign key within migration? (/showthread.php?tid=84719) |
How to add column with foreign key within migration? - jozefrebjak - 11-03-2022 I would like to add new column in database within migration, but I want to add also foreign key. I already have a table devices and I want to add column customer_id with relation to customers table. Here is a migration PHP Code: <?php But when I ran php spark migrate -all I'm getting Code: Field `customer_id` not found. and migration fail. RE: How to add column with foreign key within migration? - InsiteFX - 11-04-2022 Please Read: CodeIgniter 4 User Guide - Foreign Keys CodeIgniter 4 User Guide - Adding Foreign Keys RE: How to add column with foreign key within migration? - jozefrebjak - 11-20-2022 @InsiteFX thanks, but I can't find what I needed in docs. Here is the working example how to get it to work. I ended with alter query. PHP Code: <?php RE: How to add column with foreign key within migration? - kenjis - 11-20-2022 Yes, you need to use query() method now. The feature to add keys to existing tables will be implemented in v4.3.0. See https://github.com/codeigniter4/CodeIgniter4/blob/4.3/user_guide_src/source/changelogs/v4.3.0.rst#forge PHP Code: $this->forge->addKey(['category', 'name'], false, false, 'category_name'); |