An Introduction to Class::DBI - Cascading Deletes 32

Deleting Records from Related Tables

  • When you delete an object, any objects related to that object by 'has_many' are also deleted

  • Objects related by 'has_a' are NOT deleted

  • Remember that has_many indicates that our primary key is in somebody else's table.

    • When our object vanishes, that key is no longer valid, and the record holding it is no longer valid.

          CREATE TABLE person (              CREATE TABLE credit (
              id    INTEGER PRIMARY KEY,         id         INTEGER PRIMARY KEY,
              name  VARCHAR(255),                type       VARCHAR(255),
              birthdate INTEGER                  person_id  INTEGER,
          );                                     film_id    INTEGER
          CREATE TABLE film (
              id    INTEGER PRIMARY KEY,
              title VARCHAR(255),
              year  INTEGER
          my ($kevin_bacon) = FilmBuff::Person->search(name => 'Kevin Bacon');
          $kevin_bacon->delete();  # Deletes Kevin and any credits he has
  • Contrast this to has_a.

    • has_a means that we have someone elses primary key in our record.

    • If our record is deleted, the record whose key we hold is still valid


YAPC::Canada << Previous | Index | Next >>
Copyright © 2003 Michael Graham