Associating Models
Other than using validations to ensure consistency within our database, we can also create associations between two tables to ensure symbiotic relationships are maintained and updated. Databases maintain these relationships by creating foreign key references that hold metadata as to which table and column the foreign key is associated with. This metadata is what maintains integrity for the database. If we were to update a foreign key’s value without a proper reference, we would have to perform a separate query to update all of the rows that contained a reference to the foreign key to its new value.
For instance, we have three tables: customers
, products
, and receipts
. The receipts
table would have two columns (in addition to others) with each referencing a column on the customers
and products
table, respectively. If we wanted to update a product’s identification column, we would have to just modify the applicable product’s identification value...