在liquibase中删除表的唯一约束列时出错

时间:2020-05-02 07:39:00

标签: java spring-boot liquibase liquibase-hibernate

当我尝试删除app_user_id列时出现此错误。我确实删除了app_user_id表的外键关系。但是idk为什么我只能在运行测试用例时得到这个。

stackTrace

SEVERE 2/5/20 2:25 PM: liquibase: liquibase/mt-tenant-changelog-postgres.xml: liquibase/versions/8.0/mt-tenant-schema-and-data-8.0.xml::TREX-263-drop-column-user-related-fields::Sunil: Change Set liquibase/versions/8.0/mt-tenant-schema-and-data-8.0.xml::TREX-263-drop-column-user-related-fields::Sunil failed.  Error: Column may be referenced by "PUBLIC.UQ_DASHBOARD_SUBSCRIPTION_DASHBOARD_COMPONENT_APP_USER_FACILITY"; SQL statement:
ALTER TABLE PUBLIC.dashboard_subscription DROP COLUMN app_user_id [90083-194] [Failed SQL: ALTER TABLE PUBLIC.dashboard_subscription DROP COLUMN app_user_id]
INFO 2/5/20 2:25 PM: liquibase: liquibase/versions/8.0/mt-tenant-schema-and-data-8.0.xml::TREX-263-drop-column-user-related-fields::Sunil: Successfully released change log lock
2020-05-02 14:25:30.600  WARN 9187 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messageSource': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.brotech.product.truelocate.core.service.MultiLingualService com.brotech.product.truelocate.config.DatabaseMessageSource.multiLingualService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'multiLingualService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.brotech.product.truelocate.core.repository.MultiLingualRepository com.brotech.product.truelocate.core.service.MultiLingualServiceImpl.multiLingualRepo; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'multiLingualRepository': Cannot create inner bean '(inner bean)#3b4f1eb' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#3b4f1eb': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/brotech/product/truelocate/multitenant/DataSourceConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is liquibase.exception.MigrationFailedException: Migration failed for change set liquibase/versions/8.0/mt-tenant-schema-and-data-8.0.xml::TREX-263-drop-column-user-related-fields::Sunil:
     Reason: liquibase.exception.DatabaseException: Column may be referenced by "PUBLIC.UQ_DASHBOARD_SUBSCRIPTION_DASHBOARD_COMPONENT_APP_USER_FACILITY"; SQL statement:
ALTER TABLE PUBLIC.dashboard_subscription DROP COLUMN app_user_id [90083-194] [Failed SQL: ALTER TABLE PUBLIC.dashboard_subscription DROP COLUMN app_user_id]
2020-05-02 14:25:30.607 ERROR 9187 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messageSource': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.brotech.product.truelocate.core.service.MultiLingualService com.brotech.product.truelocate.config.DatabaseMessageSource.multiLingualService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'multiLingualService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.brotech.product.truelocate.core.repository.MultiLingualRepository com.brotech.product.truelocate.core.service.MultiLingualServiceImpl.multiLingualRepo; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'multiLingualRepository': Cannot create inner bean '(inner bean)#3b4f1eb' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#3b4f1eb': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/brotech/product/truelocate/multitenant/DataSourceConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is liquibase.exception.MigrationFailedException: Migration failed for change set liquibase/versions/8.0/mt-tenant-schema-and-data-8.0.xml::TREX-263-drop-column-user-related-fields::Sunil:
     Reason: liquibase.exception.DatabaseException: Column may be referenced by "PUBLIC.UQ_DASHBOARD_SUBSCRIPTION_DASHBOARD_COMPONENT_APP_USER_FACILITY"; SQL statement:
ALTER TABLE PUBLIC.dashboard_subscription DROP COLUMN app_user_id [90083-194] [Failed SQL: ALTER TABLE PUBLIC.dashboard_subscription DROP COLUMN app_user_id]
    org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/brotech/product/truelocate/multitenant/DataSourceConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is liquibase.exception.MigrationFailedException: Migration failed for change set liquibase/versions/8.0/mt-tenant-schema-and-data-8.0.xml::TREX-263-drop-column-user-related-fields::Sunil:
     Reason: liquibase.exception.DatabaseException: Column may be referenced by "PUBLIC.UQ_DASHBOARD_SUBSCRIPTION_DASHBOARD_COMPONENT_APP_USER_FACILITY"; SQL statement:
ALTER TABLE PUBLIC.dashboard_subscription DROP COLUMN app_user_id [90083-194] [Failed SQL: ALTER TABLE PUBLIC.dashboard_subscription DROP COLUMN app_user_id]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 40 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'multiLingualService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.brotech.product.truelocate.core.repository.MultiLingualRepository com.brotech.product.truelocate.core.service.MultiLingualServiceImpl.multiLingualRepo; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'multiLingualRepository': Cannot create inner bean '(inner bean)#3b4f1eb' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#3b4f1eb': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/brotech/product/truelocate/multitenant/DataSourceConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is liquibase.exception.MigrationFailedException: Migration failed for change set liquibase/versions/8.0/mt-tenant-schema-and-data-8.0.xml::TREX-263-drop-column-user-related-fields::Sunil:
     Reason: liquibase.exception.DatabaseException: Column may be referenced by "PUBLIC.UQ_DASHBOARD_SUBSCRIPTION_DASHBOARD_COMPONENT_APP_USER_FACILITY"; SQL statement:
ALTER TABLE PUBLIC.dashboard_subscription DROP COLUMN app_user_id [90083-194] [Failed SQL: ALTER TABLE PUBLIC.dashboard_subscription DROP COLUMN app_user_id]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1192)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
    ... 42 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.brotech.product.truelocate.core.repository.MultiLingualRepository com.brotech.product.truelocate.core.service.MultiLingualServiceImpl.multiLingualRepo; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'multiLingualRepository': Cannot create inner bean '(inner bean)#3b4f1eb' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#3b4f1eb': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/brotech/product/truelocate/multitenant/DataSourceConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is liquibase.exception.MigrationFailedException: Migration failed for change set liquibase/versions/8.0/mt-tenant-schema-and-data-8.0.xml::TREX-263-drop-column-user-related-fields::Sunil:
     Reason: liquibase.exception.DatabaseException: Column may be referenced by "PUBLIC.UQ_DASHBOARD_SUBSCRIPTION_DASHBOARD_COMPONENT_APP_USER_FACILITY"; SQL statement:
ALTER TABLE PUBLIC.dashboard_subscription DROP COLUMN app_user_id [90083-194] [Failed SQL: ALTER TABLE PUBLIC.dashboard_subscription DROP COLUMN app_user_id]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 53 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'multiLingualRepository': Cannot create inner bean '(inner bean)#3b4f1eb' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#3b4f1eb': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/brotech/product/truelocate/multitenant/DataSourceConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is liquibase.exception.MigrationFailedException: Migration failed for change set liquibase/versions/8.0/mt-tenant-schema-and-data-8.0.xml::TREX-263-drop-column-user-related-fields::Sunil:
     Reason: liquibase.exception.DatabaseException: Column may be referenced by "PUBLIC.UQ_DASHBOARD_SUBSCRIPTION_DASHBOARD_COMPONENT_APP_USER_FACILITY"; SQL statement:
ALTER TABLE PUBLIC.dashboard_subscription DROP COLUMN app_user_id [90083-194] [Failed SQL: ALTER TABLE PUBLIC.dashboard_subscription DROP COLUMN app_user_id]
    at 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/brotech/product/truelocate/multitenant/DataSourceConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is liquibase.exception.MigrationFailedException: Migration failed for change set liquibase/versions/8.0/mt-tenant-schema-and-data-8.0.xml::TREX-263-drop-column-user-related-fields::Sunil:
     Reason: liquibase.exception.DatabaseException: Column may be referenced by "PUBLIC.UQ_DASHBOARD_SUBSCRIPTION_DASHBOARD_COMPONENT_APP_USER_FACILITY"; SQL statement:
ALTER TABLE PUBLIC.dashboard_subscription DROP COLUMN app_user_id [90083-194] [Failed SQL: ALTER TABLE PUBLIC.dashboard_subscription DROP COLUMN app_user_id]

请找到下面的更改集,这是非常普通的语法:

 <dropColumn tableName="dashboard_subscription" columnName="app_user_id" />

1 个答案:

答案 0 :(得分:0)

有时候,某些ORM(例如Hibernate)或另一个数据库用户在不知情的情况下意外创建了foreignKey,索引等,这使得更改数据库架构有些棘手。

如果您的dropColumn changeSet由于存在一些现有的外键引用而失败,则可以执行以下步骤:

  • 通过某种工具(例如DBeaver / PgAdmin / etc,取决于数据库)检查数据库架构,以了解要删除的列上的任何约束或引用;
  • 如果没有帮助,请获取阻止异常堆栈跟踪中列删除的引用名称;
  • 创建并执行dropForeignKeyConstraint / dropIndex / dropUniqueConstraint / etc。 changeSet;
  • 如果该changeSet由于其他一些引用而失败,则也删除该引用;
  • 最后,您将可以删除所需的列;
相关问题