Skip to content

Cascading delete database objects

Michel Blanc edited this page Aug 11, 2012 · 1 revision

You have several options to do this. You database might support it directly, and cascade delete records references by a foreign key.

You can also do it "manually" with Sequel hooks.

For instance, let's say you can to remove all albums for an artist when you remove the artist, you can do so like this :

class Artists < Sequel::Model
  one_to_many :albums
    
  def before_destroy
    Albums.filter(:domain_id => id).destroy
  end
end

Thus, before removing an artist, all it's albums will be removed.

You can learn more about models hooks in the Sequel Hooks

Clone this wiki locally