question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`foreignKeyConstraintExists` not working

See original GitHub issue

Environment

Liquibase Version:

 ./bin/liquibase --version
####################################################
##   _     _             _ _                      ##
##  | |   (_)           (_) |                     ##
##  | |    _  __ _ _   _ _| |__   __ _ ___  ___   ##
##  | |   | |/ _` | | | | | '_ \ / _` / __|/ _ \  ##
##  | |___| | (_| | |_| | | |_) | (_| \__ \  __/  ##
##  \_____/_|\__, |\__,_|_|_.__/ \__,_|___/\___|  ##
##              | |                               ##
##              |_|                               ##
##                                                ## 
##  Get documentation at docs.liquibase.com       ##
##  Get certified courses at learn.liquibase.com  ## 
##  Free schema change activity reports at        ##
##      https://hub.liquibase.com                 ##
##                                                ##
####################################################
Starting Liquibase at 17:02:20 (version 4.7.0 #1140 built at 2022-01-07 19:26+0000)
Liquibase Version: 4.7.0
Liquibase Community 4.7.0 by Liquibase
Running Java under /usr/java/jdk1.8.0_144/jre (Version 1.8.0_144)

Note: The same Liquibase changeSet worked in Liquibase Version: 3.10.2

Liquibase Integration & Version: CLI

Liquibase Extension(s) & Version:

Database Vendor & Version:

> SELECT version();
                                   version                                    
------------------------------------------------------------------------------
 PostgreSQL 10.13 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.9.3, 64-bit

Operating System Type & Version:

$ cat /etc/os-release 
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

Description

foreignKeyConstraintExists doesn’t seem to be working. I’m trying to MARK_RAN if foreignKey already exists so I just create the FK if it is absent.

This was working with Liquibase 3.10.2. I’m in the process of upgrading it in order to improve performance as a simple migration took multiple hours with the previous version.

Steps To Reproduce

<changeSet ...>
  <preConditions onFail="MARK_RAN">
    <not>
      <foreignKeyConstraintExists foreignKeyName="FK_NAME"/>
    </not>
  </preConditions>
  <addForeignKeyConstraint constraintName="FK_NAME"  ... />
</changeSet>

Actual Behavior

changeSet execution fails trying to execute the addForeignKeyConstraint (since the constraint with that name already exists).

Expected/Desired Behavior

The precondition onFail should’ve been called (adding the changeSet as MARK_RAN to liquibase changelog), and, as such, the changeSet shouldn’t execute addForeignKeyContstraint.

Additional Context

I was able to implement a temporary workaround specific to the database vendor that confirms that the FK exists.

        <preConditions onFail="MARK_RAN">
            <sqlCheck expectedResult="0">
              SELECT COUNT(*) FROM information_schema.table_constraints WHERE constraint_name='FK_NAME' AND table_schema = '<x>'
            </sqlCheck>
        </preConditions>

From my (limited) understanding, Liquibase does some sort of DB snapshot, which it uses to verify some of these pre-conditions (not the sqlCheck) and it doesn’t seem to be importing the ForeignKey to that snapshot.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
liam-vertacommented, Apr 7, 2022

@joserebelo

I can also confirm that providing the table name serves as a workaround, at least during my tests.

Awesome, thanks.

@kataggart Thanks for taking another look.

1reaction
kataggartcommented, Apr 5, 2022

@liam-verta sorry for delay here; will raise it with the team again and see what we might be able to do. Thanks @joserebelo for the PR.

Read more comments on GitHub >

github_iconTop Results From Across the Web

foreignKeyConstraintExists Precondition Seems Not Adequate ...
It seems the database does not allow 2 foreign keys which names are different but reference details are same. Do you have any...
Read more >
ForeignKeyConstraintExists Precondition Not Working?
Is the foreign key constraint exists precondition broken on MySQL, or am I doing something wrong? Snippet from output:
Read more >
foreignKeyConstraintExists precondition fails with constraints ...
I put in a change earlier in 3.2 to handle foreign keys on unique constraints better on oracle and I am no longer...
Read more >
Preconditions | Liquibase Docs
Document what assumptions the author of the changelog had when creating it. Enforce that those assumptions are not violated by users running the...
Read more >
Error starting OpenSpecimen on Tomcat 9 - Technical Questions
<preConditions onFail="MARK_RAN"> <foreignKeyConstraintExists foreignKeyName="FKB025CFC71792AD22"/> ... This should work without any issues.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found