ForeignKeys Set in Database Snapshot contains dupes

I’m attempting to use the set of Foreign Keys available in the DatabaseSnapshot and noticed something that is probably a bug, but is at least a little confusing.

It appears that each ForeignKey object in the set has all of the column names for all FKs that reference the PK table’s primary key column names. There are the correct number of ForeignKey objects in the set for all the FKs in my database but not all of the FK constraint names or FK table names are represented.

For example, if I have 3 tables: Person, Cat, Dog and both Cat and Dog have an ownerId that references the Person table there are two FKs in my database. These FKs would be represented as:

FK1:
 name: FK_CAT_OWNER
 fk_table: Cat
 fk_columns: ownerId, ownerId
 pk_table: Person
 pk_column: id, id
FK2:
 name: FK_CAT_OWNER
 fk_table: Cat
 fk_columns: ownerId, ownerId
 pk_table: Person
 pk_column: id, id

The set would contain two duplicate FK objects both representing the FK on the Cat table pointing to the Person table. I believe this is because of the way that the set is constructed: it’s using the same FK object over and over, but changing it’s contents effectively changing the hashcode of the object and leading to duplicates in the set (see SqlDatabaseSnapshot lines 368-370 and 416-418 in readForeignKeyInformation).

This creates two problems for me:

  1. Not all FK names are represented meaning that if I used DatabaseSnapshot.getForeignKey(String foreignKeyName) there’s a chance that a valid FK constraint would not be returned
  2. Even though all the columns are listed in the FK object, you can’t match up the correct column name with the correct table name as you only have the last added FK table name even though the FK column names could be coming from multiple tables.

Am I understanding this correctly?

Hmm, I tracked down this change: http://liquibase.jira.com/source/changelog/CORE/?cs=1036 that seems to be causing the duplication in the collection of Foreign Key objects. Is 1.9.5 officially released yet? Your JIRA does not appear to indicate as much even though I was able to download it. I’ve verified that this class changed from 1.9.4 to 1.9.5.

The logic being used to determine to when to instantiate a new FKInfo object is the problem.

Would you like me to file a bug for this?

Thanks for trackign that down.  I’ll take a look at it.  1.9.5 is released, I just didn’t update jira to show that.    If you could log a bug, that would be great.

Thanks for reporting the issue
Nathan

Thanks I filed: http://liquibase.jira.com/browse/CORE-510