diff --git a/dm-constraints/lib/dm-constraints/data_objects_adapter.rb b/dm-constraints/lib/dm-constraints/data_objects_adapter.rb index b511ec78..d1c3beca 100644 --- a/dm-constraints/lib/dm-constraints/data_objects_adapter.rb +++ b/dm-constraints/lib/dm-constraints/data_objects_adapter.rb @@ -14,7 +14,7 @@ def create_constraints_statements(repository_name, model) foreign_keys = parent.key.map { |key| property_to_column_name(parent.repository(repository_name), key, false) } one_to_many_relationship = parent.relationships.values.select { |rel| rel.child_model == model }.first - delete_constraint_type = case one_to_many_relationship.delete_constraint + delete_constraint_type = case one_to_many_relationship.nil? ? :protect : one_to_many_relationship.delete_constraint when :protect, nil "NO ACTION" when :destroy, :destroy! diff --git a/dm-constraints/spec/integration/constraints_spec.rb b/dm-constraints/spec/integration/constraints_spec.rb index b31f04cb..e0709dba 100644 --- a/dm-constraints/spec/integration/constraints_spec.rb +++ b/dm-constraints/spec/integration/constraints_spec.rb @@ -43,10 +43,22 @@ class ::Cow belongs_to :farmer end + #Used to test a belongs_to association with no has() association + #on the other end + class ::Pig + include DataMapper::Resource + include DataMapper::Constraints + + property :id, Serial + property :name, String + + belongs_to :farmer + end + DataMapper.auto_migrate! end - it "is included when DataMapper::Searchable is loaded" do + it "is included when DataMapper::Constraints is loaded" do Cow.new.should be_kind_of(DataMapper::Constraints) end @@ -217,6 +229,9 @@ class ::Cow before do end + class ::Farmer + end + it "should raise an error" do lambda do class ::Farmer @@ -229,5 +244,40 @@ class ::Farmer end # describe 'constraint options' + describe "belongs_to without matching has association" do + it "should destroy the parent if there are no children in the association" do + @f1 = Farmer.create(:first_name => "John", :last_name => "Doe") + @f2 = Farmer.create(:first_name => "Some", :last_name => "Body") + @p1 = Pig.create(:name => "Bea", :farmer => @f2) + @f1.destroy.should == true + end + case adapter + when :mysql + it "should raise a MysqlError when destroying the parent if there are children in the association" do + @f = Farmer.create(:first_name => "John", :last_name => "Doe") + @p1 = Pig.create(:name => "Bea", :farmer => @f) + lambda {@f.destroy}.should raise_error(MysqlError) + end + when :postgres + it "should raise a PostgresError when destroying the parent if there are children in the association" do + @f = Farmer.create(:first_name => "John", :last_name => "Doe") + @p1 = Pig.create(:name => "Bea", :farmer => @f) + lambda {@f.destroy}.should raise_error(PostgresError) + end + else + it "should destroy the parent if there are children in the association and the adapter does not support constraints" do + @f = Farmer.create(:first_name => "John", :last_name => "Doe") + @p1 = Pig.create(:name => "Bea", :farmer => @f) + @f.destroy.should == true + end + end + + it "the child should be destroyable" do + @f = Farmer.create(:first_name => "John", :last_name => "Doe") + @p = Pig.create(:name => "Bea", :farmer => @f) + @p.destroy.should == true + end + + end # describe 'belongs_to without matching has association' end # DataMapper::Constraints end # ADAPTERS.each