codeigniter4 / CodeIgniter4

Open Source PHP Framework (originally from EllisLab)
https://codeigniter.com/
MIT License
5.39k stars 1.9k forks source link

Add custom name for ForeignKeys. #6366

Closed stausif closed 2 years ago

stausif commented 2 years ago

When create foreign keys there is set pattern in Forge class how the key name will be generated. This ends up making problem for tables and fields with long names, below [mysqli_sql_exception] is thrown. Specified key was too long; max key length is 767 bytes

Solution would be to add an options array at end in which constraint name can be defined and in code the update can be done if constraint name is defined then skip auto generation part for key name. below is where this change is recommended

Add options array at end of this function which excepts constraint name

https://github.com/codeigniter4/CodeIgniter4/blob/920488950b62ca394c3c7183a91409759deef61b/system/Database/Forge.php#L404

Skip default key name and used the constraint name passed in options array to addForeignKey method

https://github.com/codeigniter4/CodeIgniter4/blob/920488950b62ca394c3c7183a91409759deef61b/system/Database/Forge.php#L1057

kenjis commented 2 years ago

Thank you for taking time. We welcome a PR. https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/README.md

Duplicate of #5075