"Constraint already exists" 将 H2 与 liquibase 一起使用时

"Constraint already exists" when using H2 with liquibase

我正在使用 H2 作为测试数据库,并使用 Liquibase 对其进行初始化。在前几个更改日志之后,我遇到了一个错误。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: Invocation of init method failed; nested exception is liquibase.exception.LiquibaseException: liquibase.exception.MigrationFailedException: Migration failed for change set db/changelogs/changelog-create-professional-5.xml::5::admin:
     Reason: liquibase.exception.DatabaseException: Constraint "FK_USER" already exists; SQL statement:
ALTER TABLE PUBLIC.professional ADD CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES PUBLIC.user (id) [90045-200] [Failed SQL: (90045) ALTER TABLE PUBLIC.professional ADD CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES PUBLIC.user (id)]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=11=](AbstractBeanFactory.java:335) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154) ~[spring-context-5.3.18.jar:5.3.18]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:908) ~[spring-context-5.3.18.jar:5.3.18]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.18.jar:5.3.18]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:740) ~[spring-boot-2.6.6.jar:2.6.6]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:415) ~[spring-boot-2.6.6.jar:2.6.6]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) ~[spring-boot-2.6.6.jar:2.6.6]
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:136) ~[spring-boot-test-2.6.6.jar:2.6.6]
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99) ~[spring-test-5.3.18.jar:5.3.18]
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124) ~[spring-test-5.3.18.jar:5.3.18]
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:124) ~[spring-test-5.3.18.jar:5.3.18]
    at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.outputConditionEvaluationReport(SpringBootDependencyInjectionTestExecutionListener.java:53) ~[spring-boot-test-autoconfigure-2.6.6.jar:2.6.6]
    at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:46) ~[spring-boot-test-autoconfigure-2.6.6.jar:2.6.6]
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:248) ~[spring-test-5.3.18.jar:5.3.18]
    at org.springframework.test.context.junit.jupiter.SpringExtension.postProcessTestInstance(SpringExtension.java:138) ~[spring-test-5.3.18.jar:5.3.18]
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors(ClassBasedTestDescriptor.java:363) ~[junit-jupiter-engine-5.8.2.jar:5.8.2]
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.executeAndMaskThrowable(ClassBasedTestDescriptor.java:368) ~[junit-jupiter-engine-5.8.2.jar:5.8.2]
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors(ClassBasedTestDescriptor.java:363) ~[junit-jupiter-engine-5.8.2.jar:5.8.2]
    at java.base/java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:195) ~[na:na]
    at java.base/java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:177) ~[na:na]
    at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1655) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) ~[na:na]
    at java.base/java.util.stream.StreamSpliterators$WrappingSpliterator.forEachRemaining(StreamSpliterators.java:312) ~[na:na]
    at java.base/java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:735) ~[na:na]
    at java.base/java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:734) ~[na:na]
    at java.base/java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:658) ~[na:na]
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeTestInstancePostProcessors(ClassBasedTestDescriptor.java:362) ~[junit-jupiter-engine-5.8.2.jar:5.8.2]
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$instantiateAndPostProcessTestInstance(ClassBasedTestDescriptor.java:283) ~[junit-jupiter-engine-5.8.2.jar:5.8.2]
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.8.2.jar:1.8.2]
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.instantiateAndPostProcessTestInstance(ClassBasedTestDescriptor.java:282) ~[junit-jupiter-engine-5.8.2.jar:5.8.2]
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$testInstancesProvider(ClassBasedTestDescriptor.java:272) ~[junit-jupiter-engine-5.8.2.jar:5.8.2]
    at java.base/java.util.Optional.orElseGet(Optional.java:369) ~[na:na]
    at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$testInstancesProvider(ClassBasedTestDescriptor.java:271) ~[junit-jupiter-engine-5.8.2.jar:5.8.2]
    at org.junit.jupiter.engine.execution.TestInstancesProvider.getTestInstances(TestInstancesProvider.java:31) ~[junit-jupiter-engine-5.8.2.jar:5.8.2]
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$prepare[=11=](TestMethodTestDescriptor.java:102) ~[junit-jupiter-engine-5.8.2.jar:5.8.2]
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.8.2.jar:1.8.2]
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:101) ~[junit-jupiter-engine-5.8.2.jar:5.8.2]
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:66) ~[junit-jupiter-engine-5.8.2.jar:5.8.2]
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$prepare(NodeTestTask.java:123) ~[junit-platform-engine-1.8.2.jar:1.8.2]
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.8.2.jar:1.8.2]
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.prepare(NodeTestTask.java:123) ~[junit-platform-engine-1.8.2.jar:1.8.2]
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:90) ~[junit-platform-engine-1.8.2.jar:1.8.2]
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1541) ~[na:na]
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41) ~[junit-platform-engine-1.8.2.jar:1.8.2]
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:155) ~[junit-platform-engine-1.8.2.jar:1.8.2]
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.8.2.jar:1.8.2]
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:141) ~[junit-platform-engine-1.8.2.jar:1.8.2]
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) ~[junit-platform-engine-1.8.2.jar:1.8.2]
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:139) ~[junit-platform-engine-1.8.2.jar:1.8.2]
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.8.2.jar:1.8.2]
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138) ~[junit-platform-engine-1.8.2.jar:1.8.2]
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95) ~[junit-platform-engine-1.8.2.jar:1.8.2]
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1541) ~[na:na]
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41) ~[junit-platform-engine-1.8.2.jar:1.8.2]
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:155) ~[junit-platform-engine-1.8.2.jar:1.8.2]
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.8.2.jar:1.8.2]
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:141) ~[junit-platform-engine-1.8.2.jar:1.8.2]
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) ~[junit-platform-engine-1.8.2.jar:1.8.2]
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:139) ~[junit-platform-engine-1.8.2.jar:1.8.2]
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.8.2.jar:1.8.2]
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138) ~[junit-platform-engine-1.8.2.jar:1.8.2]
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95) ~[junit-platform-engine-1.8.2.jar:1.8.2]
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35) ~[junit-platform-engine-1.8.2.jar:1.8.2]
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57) ~[junit-platform-engine-1.8.2.jar:1.8.2]
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54) ~[junit-platform-engine-1.8.2.jar:1.8.2]
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:107) ~[junit-platform-launcher-1.8.2.jar:1.8.2]
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88) ~[junit-platform-launcher-1.8.2.jar:1.8.2]
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute[=11=](EngineExecutionOrchestrator.java:54) ~[junit-platform-launcher-1.8.2.jar:1.8.2]
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67) ~[junit-platform-launcher-1.8.2.jar:1.8.2]
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52) ~[junit-platform-launcher-1.8.2.jar:1.8.2]
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114) ~[junit-platform-launcher-1.8.2.jar:1.8.2]
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:86) ~[junit-platform-launcher-1.8.2.jar:1.8.2]
    at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:86) ~[junit-platform-launcher-1.8.2.jar:1.8.2]
    at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:53) ~[junit-platform-launcher-1.8.2.jar:1.8.2]
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:71) ~[junit5-rt.jar:na]
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.execute(IdeaTestRunner.java:38) ~[junit-rt.jar:na]
    at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11) ~[idea_rt.jar:na]
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35) ~[junit-rt.jar:na]
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235) ~[junit-rt.jar:na]
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54) ~[junit-rt.jar:na]
Caused by: liquibase.exception.LiquibaseException: liquibase.exception.MigrationFailedException: Migration failed for change set db/changelogs/changelog-create-professional-5.xml::5::admin:
     Reason: liquibase.exception.DatabaseException: Constraint "FK_USER" already exists; SQL statement:
ALTER TABLE PUBLIC.professional ADD CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES PUBLIC.user (id) [90045-200] [Failed SQL: (90045) ALTER TABLE PUBLIC.professional ADD CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES PUBLIC.user (id)]
    at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:124) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.Liquibase.lambda$null[=11=](Liquibase.java:265) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.Scope.lambda$child[=11=](Scope.java:177) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.Scope.child(Scope.java:186) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.Scope.child(Scope.java:176) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.Scope.child(Scope.java:155) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.Scope.child(Scope.java:239) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.Liquibase.lambda$update(Liquibase.java:264) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.Scope.lambda$child[=11=](Scope.java:177) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.Scope.child(Scope.java:186) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.Scope.child(Scope.java:176) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.Scope.child(Scope.java:155) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.Liquibase.runInScope(Liquibase.java:2404) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.Liquibase.update(Liquibase.java:211) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.Liquibase.update(Liquibase.java:197) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.integration.spring.SpringLiquibase.performUpdate(SpringLiquibase.java:314) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:269) ~[liquibase-core-4.5.0.jar:na]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.18.jar:5.3.18]
    ... 88 common frames omitted
Caused by: liquibase.exception.MigrationFailedException: Migration failed for change set db/changelogs/changelog-create-professional-5.xml::5::admin:
     Reason: liquibase.exception.DatabaseException: Constraint "FK_USER" already exists; SQL statement:
ALTER TABLE PUBLIC.professional ADD CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES PUBLIC.user (id) [90045-200] [Failed SQL: (90045) ALTER TABLE PUBLIC.professional ADD CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES PUBLIC.user (id)]
    at liquibase.changelog.ChangeSet.execute(ChangeSet.java:695) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.changelog.visitor.UpdateVisitor.visit(UpdateVisitor.java:49) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.changelog.ChangeLogIterator.lambda$null[=11=](ChangeLogIterator.java:111) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.Scope.lambda$child[=11=](Scope.java:177) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.Scope.child(Scope.java:186) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.Scope.child(Scope.java:176) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.Scope.child(Scope.java:155) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.changelog.ChangeLogIterator.lambda$run(ChangeLogIterator.java:110) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.Scope.lambda$child[=11=](Scope.java:177) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.Scope.child(Scope.java:186) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.Scope.child(Scope.java:176) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.Scope.child(Scope.java:155) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.Scope.child(Scope.java:239) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:94) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.Scope.lambda$child[=11=](Scope.java:177) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.Scope.child(Scope.java:186) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.Scope.child(Scope.java:176) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.Scope.child(Scope.java:155) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.Scope.child(Scope.java:239) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.Scope.child(Scope.java:243) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:66) ~[liquibase-core-4.5.0.jar:na]
    ... 106 common frames omitted
Caused by: liquibase.exception.DatabaseException: Constraint "FK_USER" already exists; SQL statement:
ALTER TABLE PUBLIC.professional ADD CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES PUBLIC.user (id) [90045-200] [Failed SQL: (90045) ALTER TABLE PUBLIC.professional ADD CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES PUBLIC.user (id)]
    at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:393) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:82) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:150) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.database.AbstractJdbcDatabase.execute(AbstractJdbcDatabase.java:1279) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.database.AbstractJdbcDatabase.executeStatements(AbstractJdbcDatabase.java:1261) ~[liquibase-core-4.5.0.jar:na]
    at liquibase.changelog.ChangeSet.execute(ChangeSet.java:660) ~[liquibase-core-4.5.0.jar:na]
    ... 126 common frames omitted
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Constraint "FK_USER" already exists; SQL statement:
ALTER TABLE PUBLIC.professional ADD CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES PUBLIC.user (id) [90045-200]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:576) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:429) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.message.DbException.get(DbException.java:205) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.message.DbException.get(DbException.java:181) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.command.ddl.AlterTableAddConstraint.tryUpdate(AlterTableAddConstraint.java:114) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.command.ddl.AlterTableAddConstraint.update(AlterTableAddConstraint.java:78) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.command.CommandContainer.update(CommandContainer.java:198) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.command.Command.executeUpdate(Command.java:251) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.jdbc.JdbcStatement.executeInternal(JdbcStatement.java:228) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.jdbc.JdbcStatement.execute(JdbcStatement.java:201) ~[h2-1.4.200.jar:1.4.200]
    at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:389) ~[liquibase-core-4.5.0.jar:na]
    ... 131 common frames omitted

这是似乎有问题的变更集:

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd
    http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
    <changeSet author="admin" id="5">
        <createTable tableName="professional">
            <column autoIncrement="true" name="id" type="BIGINT">
                <constraints primaryKey="true"/>
            </column>
            <column name="clinic_id" type="BIGINT">
                <constraints nullable="false" />
            </column>
            <column name="user_id" type="BIGINT">
                <constraints nullable="false" />
            </column>
            <column name="title" type="VARCHAR(5)"/>
            <column name="last_name" type="VARCHAR(50)">
                <constraints nullable="false" />
            </column>
            <column name="first_name" type="VARCHAR(50)">
                <constraints nullable="false" />
            </column>
            <column name="birth_date" type="DATE">
                <constraints nullable="false" />
            </column>
            <column name="email" type="VARCHAR(254)">
                <constraints nullable="false" />
            </column>
        </createTable>
        <addForeignKeyConstraint constraintName="fk_user"
            baseTableName="professional" baseColumnNames="user_id"
            referencedTableName="user" referencedColumnNames="id" />
        <addForeignKeyConstraint constraintName="fk_clinic"
            baseTableName="professional" baseColumnNames="clinic_id"
            referencedTableName="clinic" referencedColumnNames="id" />
        <rollback>
            <dropTable tableName="professional"/>
        </rollback>
    </changeSet>
</databaseChangeLog>

奇怪的是,有一些变更集,其中有外键约束,并且初始化成功。例如:

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd
    http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
    <changeSet author="admin" id="4">
        <createTable tableName="patient">
            <column autoIncrement="true" name="id" type="BIGINT">
                <constraints primaryKey="true"/>
            </column>
            <column name="clinic_id" type="BIGINT">
                <constraints nullable="false" />
            </column>
            <column name="user_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
            <column name="patient_id" type="VARCHAR(30)">
                <constraints nullable="false" />
            </column>
            <column name="insurance_number" type="VARCHAR(30)">
                <constraints nullable="false" />
            </column>
            <column name="title" type="VARCHAR(5)"/>
            <column name="last_name" type="VARCHAR(50)">
                <constraints nullable="false" />
            </column>
            <column name="first_name" type="VARCHAR(50)">
                <constraints nullable="false" />
            </column>
            <column name="birth_date" type="DATE">
                <constraints nullable="false" />
            </column>
            <column name="sex" type="VARCHAR(5)">
                <constraints nullable="false" />
            </column>
            <column name="phone_number" type="VARCHAR(10)">
                <constraints nullable="false" />
            </column>
            <column name="email" type="VARCHAR(254)"/>
            <column name="comment" type="VARCHAR(255)"/>
        </createTable>
        <addForeignKeyConstraint constraintName="fk_user"
            baseTableName="patient" baseColumnNames="user_id"
            referencedTableName="user" referencedColumnNames="id" />
        <addForeignKeyConstraint constraintName="fk_clinic"
            baseTableName="patient" baseColumnNames="clinic_id"
            referencedTableName="clinic" referencedColumnNames="id" />
        <rollback>
            <dropTable tableName="patient"/>
        </rollback>
    </changeSet>
</databaseChangeLog>

产品数据库 (PostgreSQL) 中一切正常。我做错了什么?

            <addForeignKeyConstraint constraintName="fk_user"
            baseTableName="professional" baseColumnNames="user_id"
            referencedTableName="user" referencedColumnNames="id" />

您使用的名称 constraintName="fk_user" 非常模糊。它比另一个 table 再次引用 User table 并使用具有相同名称的约束 fk_user 是非常可能的。约束名称必须是唯一的,因此请将此 constraintName 更改为对该架构唯一的名称。

您可以为存在约束的 table professional 使用特定名称。例如 fk_professional_user.

您在其他更改日志中看到的其他外键约束可能不存在于其他具有相同名称的 table 下。