lib/activefacts/persistence/foreignkey.rb in activefacts-0.7.1 vs lib/activefacts/persistence/foreignkey.rb in activefacts-0.7.2

- old
+ new

@@ -33,11 +33,15 @@ class Concept # When an EntityType is fully absorbed, its foreign keys are too. # Return an Array of Reference paths for such absorbed FKs def all_absorbed_foreign_key_reference_path references_from.inject([]) do |array, ref| + if ref.is_simple_reference + # This catches references that would be created to secondary supertypes, when absorption is through primary. + # There might be other cases where an exclusion like this is needed, but I can't reason it out. + next array if TypeInheritance === ref.fact_type && absorbed_via && TypeInheritance === absorbed_via.fact_type array << [ref] elsif ref.is_absorbing ref.to.all_absorbed_foreign_key_reference_path.each{|aref| array << aref.insert(0, ref) } @@ -100,11 +104,23 @@ c end end debug :fk, "to_columns in #{to.name}: #{to_columns.map { |column| column ? column.name : "OOPS!" }*", "}" - ActiveFacts::Persistence::ForeignKey.new(self, to, fk_ref_path[-1], from_columns, to_columns) + # Put the column pairs in a defined order, sorting key pairs by to-name: + froms, tos = from_columns.zip(to_columns).sort_by { |pair| + pair[1].name(nil) + }.transpose + + ActiveFacts::Persistence::ForeignKey.new(self, to, fk_ref_path[-1], froms, tos) end + end. + sort_by do |fk| + # Put the foreign keys in a defined order: + [ fk.to.name, + fk.to_columns.map{|col| col.name(nil).sort}, + fk.from_columns.map{|col| col.name(nil).sort} + ] end end end end end