# Possible bug in ForeignKeyComparator.isSameObject() introduced by 3.0.3 release.

**URL:** <https://forum.liquibase.org/t/possible-bug-in-foreignkeycomparator-issameobject-introduced-by-3-0-3-release/2687>\
**Category:** Liquibase Development\
**Created:** [August 31, 2013, 1:19am UTC](https://forum.liquibase.org/t/possible-bug-in-foreignkeycomparator-issameobject-introduced-by-3-0-3-release/2687 "2013-08-31T01:19:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guest](https://avatars.discourse-cdn.com/v4/letter/g/8dc957/32.png) [@Guest](https://forum.liquibase.org/u/Guest)\
**Post date:** [August 31, 2013, 1:19am UTC](https://forum.liquibase.org/t/possible-bug-in-foreignkeycomparator-issameobject-introduced-by-3-0-3-release/2687/1 "2013-08-31T01:19:00Z")

</div>

There appears to be a bug in:  
liquibase-core/src/main/java/liquibase/diff/compare/core/ForeignKeyComparator.java  
  
Which was changed on 8/27/2013 for the 3.0.3 release.  
  
If you have a precondition like this:  
_&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_   
  
  
You will encounter this error since the ForeignKeyColumns and PrimaryKeyColumns aren’t set in the change set:  
  
_Caused by: liquibase.exception.MigrationFailedException: Migration failed for change set foo.xml::xxx:💤  
&nbsp;&nbsp;&nbsp;&nbsp; Reason:   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; src/main/resources/db/activFoundation.xml : liquibase.precondition.core.ForeignKeyExistsPrecondition@5f96f335 : java.lang.NullPointerException_  
  
_&nbsp;&nbsp;&nbsp; at liquibase.changelog.ChangeSet.execute(ChangeSet.java:310)  
&nbsp;&nbsp;&nbsp; at liquibase.changelog.visitor.UpdateVisitor.visit(UpdateVisitor.java:28)  
&nbsp;&nbsp;&nbsp; at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:58)  
&nbsp;&nbsp;&nbsp; at liquibase.Liquibase.update(Liquibase.java:135)  
&nbsp;&nbsp;&nbsp; at org.liquibase.maven.plugins.LiquibaseUpdate.doUpdate(LiquibaseUpdate.java:31)  
&nbsp;&nbsp;&nbsp; at org.liquibase.maven.plugins.AbstractLiquibaseUpdateMojo.performLiquibaseTask(AbstractLiquibaseUpdateMojo.java:24)  
&nbsp;&nbsp;&nbsp; at org.liquibase.maven.plugins.AbstractLiquibaseMojo.execute(AbstractLiquibaseMojo.java:375)  
&nbsp;&nbsp;&nbsp; … 21 more  
Caused by: liquibase.exception.PreconditionErrorException: Precondition Error  
&nbsp;&nbsp;&nbsp; at liquibase.precondition.core.ForeignKeyExistsPrecondition.check(ForeignKeyExistsPrecondition.java:76)  
&nbsp;&nbsp;&nbsp; at liquibase.precondition.core.NotPrecondition.check(NotPrecondition.java:30)  
&nbsp;&nbsp;&nbsp; at liquibase.precondition.core.AndPrecondition.check(AndPrecondition.java:34)  
&nbsp;&nbsp;&nbsp; at liquibase.precondition.core.PreconditionContainer.check(PreconditionContainer.java:199)  
&nbsp;&nbsp;&nbsp; at liquibase.changelog.ChangeSet.execute(ChangeSet.java:274)  
&nbsp;&nbsp;&nbsp; … 27 more_  
  
  
I downloaded and changed the code to this and rebuilt locally and the issue is resolved.&nbsp; The change is checking the value of getForeignKeyColumns() and getPrimaryKeyColumns() for&nbsp; null before doing the comparison.  
  
&nbsp;&nbsp;&nbsp; public boolean isSameObject(DatabaseObject databaseObject1, DatabaseObject databaseObject2, Database accordingTo, DatabaseObjectComparatorChain chain) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!(databaseObject1 instanceof ForeignKey && databaseObject2 instanceof ForeignKey)) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ForeignKey thisForeignKey = (ForeignKey) databaseObject1;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ForeignKey otherForeignKey = (ForeignKey) databaseObject2;  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (thisForeignKey.getName() != null && otherForeignKey.getName() != null) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (chain.isSameObject(thisForeignKey, otherForeignKey, accordingTo)) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return true;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (thisForeignKey.getForeignKeyColumns() != null && thisForeignKey.getPrimaryKeyColumns() != null &&  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; otherForeignKey.getForeignKeyColumns() != null && otherForeignKey.getPrimaryKeyColumns() != null) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; boolean columnsTheSame;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (accordingTo.isCaseSensitive()) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; columnsTheSame = StringUtils.trimToEmpty(thisForeignKey.getForeignKeyColumns()).equals(StringUtils.trimToEmpty(otherForeignKey.getForeignKeyColumns())) &&  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; StringUtils.trimToEmpty(thisForeignKey.getPrimaryKeyColumns()).equals(StringUtils.trimToEmpty(otherForeignKey.getPrimaryKeyColumns()));  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; columnsTheSame = thisForeignKey.getForeignKeyColumns().equalsIgnoreCase(otherForeignKey.getForeignKeyColumns()) &&  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; thisForeignKey.getPrimaryKeyColumns().equalsIgnoreCase(otherForeignKey.getPrimaryKeyColumns());  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return columnsTheSame &&  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DatabaseObjectComparatorFactory.getInstance().isSameObject(thisForeignKey.getForeignKeyTable(), otherForeignKey.getForeignKeyTable(), accordingTo) &&  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DatabaseObjectComparatorFactory.getInstance().isSameObject(thisForeignKey.getPrimaryKeyTable(), otherForeignKey.getPrimaryKeyTable(), accordingTo);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //false by default  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;  
&nbsp;&nbsp;&nbsp; }  
  
I’m not sure this is the optimal solution, but certainly works.  
  
Thanks in advance for help with this.  
  
Andy

---

<div class="post-metadata">

**Author:** ![nvoxland](https://avatars.discourse-cdn.com/v4/letter/n/87869e/32.png) [@nvoxland](https://forum.liquibase.org/u/nvoxland)\
**Post date:** [August 31, 2013, 1:19am UTC](https://forum.liquibase.org/t/possible-bug-in-foreignkeycomparator-issameobject-introduced-by-3-0-3-release/2687/2 "2013-08-31T01:19:00Z")

</div>

Thanks for the report and the code change. I incorporated your change with a small fix and that seems to solve the problem for me. I’ll make sure there are no other issues with 3.0.3 and release 3.0.4 with the fix later this week.

Nathan

---

<div class="post-metadata">

**Author:** ![Guest](https://avatars.discourse-cdn.com/v4/letter/g/8dc957/32.png) [@Guest](https://forum.liquibase.org/u/Guest)\
**Post date:** [August 31, 2013, 1:19am UTC](https://forum.liquibase.org/t/possible-bug-in-foreignkeycomparator-issameobject-introduced-by-3-0-3-release/2687/3 "2013-08-31T01:19:00Z")

</div>

got a nullpointer here too,

thanks for the quick response. liquibase rocks!
