During development, one often violates the referential integrity: an id references a record that no longer exist. You have for example a model which belongs to a another model (let us say class Event belongs_to :place
), which means you have a field place_id in the events table, and some of these ids may be invalid. In this case, you can determine if the referential integrity is broken by a LEFT OUTER JOIN between both tables, which returns the events with invalid place:
SELECT * FROM events LEFT JOIN places ON places.id=events.place_id where places.id IS NULL
They can be deleted with the following SQL statement
DELETE events,places FROM events LEFT JOIN places ON places.id=events.place_id where places.id IS NULL