# AddForeignKeyConstraint Issue

**URL:** <https://forum.liquibase.org/t/addforeignkeyconstraint-issue/406>\
**Category:** General Discussion\
**Created:** [December 21, 2009, 4:03pm UTC](https://forum.liquibase.org/t/addforeignkeyconstraint-issue/406 "2009-12-21T16:03:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bpaye1](https://avatars.discourse-cdn.com/v4/letter/b/7feea3/32.png) [@bpaye1](https://forum.liquibase.org/u/bpaye1)\
**Post date:** [December 21, 2009, 4:03pm UTC](https://forum.liquibase.org/t/addforeignkeyconstraint-issue/406/1 "2009-12-21T16:03:00Z")

</div>

When I add a foreign key constaint on a table, liquibase generates and executes a drop constraint statement before creating the constraint. The liquibase scripts fails when the constaint does not already exist.

I am using db2 expresss.

Any help is appreciated.

Here is the xml i am using:

---

<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:** [December 21, 2009, 4:03pm UTC](https://forum.liquibase.org/t/addforeignkeyconstraint-issue/406/2 "2009-12-21T16:03:00Z")

</div>

I’m not sure why it is trying to drop it.&nbsp; What version of liquibase are you using?&nbsp;

If you change the tag to be a tag and specify the sql you want it to use, does it work?

Nathan

---

<div class="post-metadata">

**Author:** ![bpaye1](https://avatars.discourse-cdn.com/v4/letter/b/7feea3/32.png) [@bpaye1](https://forum.liquibase.org/u/bpaye1)\
**Post date:** [December 21, 2009, 4:03pm UTC](https://forum.liquibase.org/t/addforeignkeyconstraint-issue/406/3 "2009-12-21T16:03:00Z")

</div>

We are using version 1.9.5. Changing to use the tags will probably resolve my issue.

---

<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:** [December 21, 2009, 4:03pm UTC](https://forum.liquibase.org/t/addforeignkeyconstraint-issue/406/4 "2009-12-21T16:03:00Z")

</div>

Let me know if the tag works.&nbsp; Like I said, I don’t see how the addForeignKeyConstraint tag should be generating teh SQL you are seeing.&nbsp; Converting to sql would show for sure if the drop statement is created by the addFK tag or something else.

Nathan&nbsp;

---

<div class="post-metadata">

**Author:** ![ravi116](https://avatars.discourse-cdn.com/v4/letter/r/91b2a8/32.png) [@ravi116](https://forum.liquibase.org/u/ravi116)\
**Post date:** [December 21, 2009, 4:03pm UTC](https://forum.liquibase.org/t/addforeignkeyconstraint-issue/406/5 "2009-12-21T16:03:00Z")

</div>

The Sql tag did not work. Unfortunately, dropping schema in db2 is not an easy task. My ideal scenario includes recreating a database from scratch. How can I get past this without running into the drop constraint issue.  
Can I add a precondition tag around the addForeignKeyConstraint tag ?

---

<div class="post-metadata">

**Author:** ![ravi116](https://avatars.discourse-cdn.com/v4/letter/r/91b2a8/32.png) [@ravi116](https://forum.liquibase.org/u/ravi116)\
**Post date:** [December 21, 2009, 4:03pm UTC](https://forum.liquibase.org/t/addforeignkeyconstraint-issue/406/6 "2009-12-21T16:03:00Z")

</div>

I am using 1.9.5 version.

---

<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:** [December 21, 2009, 4:03pm UTC](https://forum.liquibase.org/t/addforeignkeyconstraint-issue/406/7 "2009-12-21T16:03:00Z")

</div>

yes, you should be able to add a precondition to the changeSet to check for the existence of the foreign key.

What is failing with the SQL?&nbsp; Is it something you could do from the database SQL command line?&nbsp; The same SQL should work in liquibase.

Nathan
