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:
- 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
- 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?