MySQL Foreign Keys: Compare Integer Type and Signedness

First distinguish failure to create a foreign key from an INSERT rejected by an existing one. The former calls for definition checks such as types, indexes and engines; the latter calls for checking the actual value and referenced parent row.

SHOW CREATE TABLE components;
SHOW CREATE TABLE component_checks;

Replace the example table names and compare the database’s current definitions rather than only ORM models. MySQL 8.4 foreign-key rules require matching size and sign for fixed-precision types such as integers. A universal claim that string lengths must match is incorrect; character sets and collations have separate requirements.

Reason through three schema combinations

For illustration, an INT UNSIGNED parent with a BIGINT UNSIGNED child differs in integer size. An INT UNSIGNED parent with an INT child differs in sign. Even when both are INT UNSIGNED, referenced indexes, engines and existing data still matter. Matching types alone does not guarantee successful constraint creation.

Choose a correction based on existing ID ranges and future requirements. Narrowing a child type can overflow existing values; widening a parent can affect other referencing tables, indexes and locking cost. Disabling foreign_key_checks does not repair the type contract.

In staging, validate backup and restoration procedures, then test DDL and both valid and invalid references. Preserve the resulting SHOW CREATE TABLE and migration record. This article analyzes documentation and illustrative definitions; no production ALTER was executed.