Created
April 27, 2010 08:54
-
-
Save whalec/380508 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreateMedia < ActiveRecord::Migration | |
def self.up | |
create_table :media do |t| | |
t.string :name, :null => false | |
t.text :description | |
t.references :asset, :null => false | |
t.timestamps | |
end | |
execute <<-EOS | |
ALTER TABLE media | |
ADD FOREIGN KEY (asset_id) | |
REFERENCES assets (id) | |
ON DELETE CASCADE | |
ON UPDATE CASCADE; | |
EOS | |
end | |
def self.down | |
drop_table :medias | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Delete ignores AR callbacks | |
it "should cascade delete the asset if deleted" do | |
asset = @media.asset | |
@media.delete | |
asset.reload.should be_nil | |
end |
you're expecting the cascade to go the wrong way - media BELONGS TO asset. If you delete the asset, the media will be deleted. Since media is a child of asset, deleting media will not delete the asset.
Ahh right of course. Cool thanks I've got it now.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Problem being that Postgres is NOT cascade deleting the asset upon delete?