Bug 37000: (follow-up) Add foreign key last

Certain configurations of MySQL will not allow a column to be changed
from nullable to non-nullable if the column has a foreign key constraint.
Add the foreign key constraint last to avoid issues from this.

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
(cherry picked from commit 8e788af50b)
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
This commit is contained in:
Emily Lamancusa 2024-06-07 16:24:26 -04:00 committed by Lucas Gass
parent a4d7767fa4
commit 796300926c
1 changed files with 12 additions and 3 deletions

View File

@ -12,9 +12,8 @@ return {
$dbh->do(
q{
ALTER TABLE bookings
ADD COLUMN `pickup_library_id` varchar(10) DEFAULT NULL COMMENT 'Identifier for booking pickup library' AFTER `item_id`,
ADD CONSTRAINT `bookings_ibfk_4` FOREIGN KEY (`pickup_library_id`) REFERENCES `branches` (`branchcode`) ON DELETE CASCADE ON UPDATE CASCADE
}
ADD COLUMN `pickup_library_id` varchar(10) DEFAULT NULL COMMENT 'Identifier for booking pickup library' AFTER `item_id`
}
) == 1
&& say_success( $out, "Added column 'bookings.pickup_library_id'" );
@ -59,5 +58,15 @@ return {
}
) == 1 && say_success( $out, "Updated column 'bookings.pickup_library_id' to NOT NULL" );
}
unless ( foreign_key_exists( 'bookings', 'bookings_ibfk_4' ) ) {
$dbh->do(
q{
ALTER TABLE bookings
ADD CONSTRAINT `bookings_ibfk_4` FOREIGN KEY (`pickup_library_id`) REFERENCES `branches` (`branchcode`) ON DELETE CASCADE ON UPDATE CASCADE
}
) == 1
&& say_success( $out, "Added foreign key 'bookings_ibfk_4' to column 'bookings.pickup_library_id'" );
}
},
};