Here we are discussing to make a column to a foreign key in your table by installer.
Add following code in your “InstallSchema.php” file:
Here, “wk_child_table” is a name of foreign key table .
“wk_main_table” is a name of parent table/reference table.
“main_id” is a reference column name of “wk_main_table” table.
“entity_id” is a foreign key column name of “wk_child_table” table.
“ACTION_CASCADE” : is a type of action which executes on a matched row of child table when a parent table row get updated or deleted
Here are list of types which can be add/define:
- ACTION_CASCADE: It automatically update or delete the matching child row(foreign key row) when parent table row get updated or deleted.
- ACTION_SET_NULL: It sets the value to null of foreign key column or columns when parent table row get updated or deleted.
- ACTION_NO_ACTION: When a parent table’s column get deleted or updated then there is no action is performed on child column.
- ACTION_RESTRICT: Rejects the update or delete operation, it means no changes happens when parent table row get deleted or updated. This action is used as default action.
- ACTION_SET_DEFAULT: it is working same as ACTION_SET_NULL, difference is it sets column’s default value when a parent row get update or delete.
Note: Type of both the columns reference column and foreign key column should be same.
After using this code foreign key will be added to your table’s column.