NAME
DBIO::MySQL::Diff::ForeignKey - Diff operations for MySQL/MariaDB foreign keys
VERSION
version 0.900004
DESCRIPTION
Represents a foreign key diff operation: ADD CONSTRAINT or DROP FOREIGN KEY. FKs that already exist on a brand-new table are created inline by DBIO::MySQL::Diff::Table (but see below), and those of a table that is dropped go with its DROP TABLE (but see below) -- this module otherwise handles FK changes on tables that exist in both source and target.
FK identity is by constraint_name. A definition change becomes a drop-then-add pair (MySQL has no ALTER FOREIGN KEY); the drop of such a pair answers true to replaced, so "classify" in DBIO::Diff::Base counts it as changing, not as a removal that needs allow_destructive. The names are the fixed ones DBIO::MySQL::DDL gives (<table>_<columns>_fk, "foreign_key_name" in DBIO::MySQL::DDL), so an install and an upgrade agree on them; a constraint the server named <table>_ibfk_N is dropped and added again under its fixed name by the next upgrade.
The drops stand first in a diff, before the tables and the indexes (DBIO::MySQL::Diff does that), and the adds last. The server refuses to drop the table a constraint references, and the index it needs, while the constraint is there. So a constraint of a table that is dropped, into another table that is dropped as well, gets its DROP FOREIGN KEY too, and the two DROP TABLE statements are free of each other. The adds include the foreign keys of a table the diff creates that need an index the same diff creates: DBIO::MySQL::Diff takes those out of the CREATE TABLE, which the server would refuse; and those whose columns an index the diff creates on the new table covers, which the server would give a second index.
METHODS
diff
my @ops = DBIO::MySQL::Diff::ForeignKey->diff(
$source_fks, $target_fks, $source_tables, $target_tables,
);
$source_fks and $target_fks are the foreign_keys sections of the two models, $source_tables and $target_tables their tables sections. It is the tables that say which table is in both models; a section of foreign keys cannot, because it has no entry for a table without one ("fetch" in DBIO::MySQL::Introspect::ForeignKeys). A table that is in both models and has lost every foreign key it had gets a DROP FOREIGN KEY for each of them, whether $target_fks names it or not.
Without $target_tables, the tables $target_fks names are taken for the tables of the target. A table with foreign keys in $source_fks only then counts as dropped, and nothing is written for it.
refuse_set_default
DBIO::MySQL::Diff::ForeignKey::refuse_set_default($table, $constraint, $fk_info);
Throws a DBIO::Exception naming table, constraint, clause and value when the on_delete or on_update of $fk_info is SET DEFAULT. Both servers take that DDL and never run the action (MySQL acts as RESTRICT, MariaDB stores RESTRICT), so it is refused where the diff writes a foreign key -- here and in DBIO::MySQL::Diff::Table -- the same way DBIO::MySQL::DDL refuses it. This is the only check of the actions the diff makes: any other value is the introspected one and is written as it stands.
as_sql
The ALTER TABLE statement. The names of the constraint, of both tables and of their columns are quoted with "quote_ident" in DBIO::MySQL::Identifier. A SET DEFAULT action throws ("refuse_set_default").
summary
AUTHOR
DBIO & DBIx::Class Authors
COPYRIGHT AND LICENSE
Copyright (C) 2026 DBIO Authors Portions Copyright (C) 2005-2025 DBIx::Class Authors Based on DBIx::Class, heavily modified.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.