def create_relationship_constraint(relationship)
return false unless valid_relationship_for_constraint?(relationship)
source_storage_name = relationship.source_model.storage_name(name)
target_storage_name = relationship.target_model.storage_name(name)
constraint_name = constraint_name(source_storage_name, relationship.name)
return false if constraint_exists?(source_storage_name, constraint_name)
constraint_type =
case relationship.inverse.constraint
when :protect then 'NO ACTION'
when :destroy, :destroy! then 'CASCADE'
when :set_nil then 'SET NULL'
end
return false if constraint_type.nil?
source_keys = relationship.source_key.map { |p| property_to_column_name(p, false) }
target_keys = relationship.target_key.map { |p| property_to_column_name(p, false) }
create_constraints_statement = create_constraints_statement(
constraint_name,
constraint_type,
source_storage_name,
source_keys,
target_storage_name,
target_keys)
execute(create_constraints_statement)
end