Liquibase 主键在 H2 上创建了两次
Posted
技术标签:
【中文标题】Liquibase 主键在 H2 上创建了两次【英文标题】:Liquibase primary key is created twice on H2 【发布时间】:2015-12-14 22:19:07 【问题描述】:一开始的问题只是 H2 数据库。在其他数据库(mysql)上它工作得很好,没有问题。
由 (jHipster) 生成的配置:
个人资料:开发
spring:
profiles:
active: dev
datasource:
dataSourceClassName: com.mysql.jdbc.jdbc2.optional.MysqlDataSource
url: jdbc:mysql://******.net.net:3306/*****_pandemicd?autoReconnect=true
databaseName: ****************
serverName: ******.net
username: ****************
password: ****************
cachePrepStmts: true
prepStmtCacheSize: 250
prepStmtCacheSqlLimit: 2048
useServerPrepStmts: true
jpa:
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
database: MYSQL
openInView: true
show_sql: true
generate-ddl: false
hibernate:
ddl-auto: none
naming-strategy: org.hibernate.cfg.EJB3NamingStrategy
properties:
hibernate.cache.use_second_level_cache: true
hibernate.cache.use_query_cache: false
hibernate.generate_statistics: true
hibernate.cache.region.factory_class: pl.sedzisz.pandemic.config.hazelcast.HazelcastCacheRegionFactory
hibernate.cache.use_minimal_puts: true
hibernate.cache.hazelcast.use_lite_member: true
配置文件:测试(在内存中)
spring:
datasource:
dataSourceClassName: org.h2.jdbcx.JdbcDataSource
url: jdbc:h2:mem:pandemic;DB_CLOSE_DELAY=-1;MODE=MySQL;TRACE_LEVEL_FILE=4
databaseName:
serverName:
username:
password:
jpa:
database-platform: org.hibernate.dialect.H2Dialect
database: H2
openInView: true
show_sql: true
generate-ddl: false
hibernate:
ddl-auto: none
naming-strategy: org.hibernate.cfg.EJB3NamingStrategy
properties:
hibernate.cache.use_second_level_cache: false
hibernate.cache.use_query_cache: false
hibernate.generate_statistics: false
hibernate.hbm2ddl.auto: validate
现在 Liquibase 更新日志:
<changeSet author="newbie" id="1449941926339-1">
<createTable tableName="ENUM_DESCENT">
<column name="descent_of_disease" type="VARCHAR(255)">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>
<changeSet author="newbie" id="1449941926339-2">
<loadData encoding="UTF-8"
file="config/liquibase/descent_of_disease.csv"
separator=";"
tableName="ENUM_DESCENT"/>
</changeSet>
<changeSet author="newbie" id="1449941926339-3">
<createTable tableName="DISEASE_DESCENTS">
<column name="disease_id" type="BIGINT">
<constraints nullable="false"/>
</column>
<column name="descent_descent_of_disease" type="VARCHAR(255)">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>
<changeSet author="newbie" id="1449941926339-4">
<addPrimaryKey columnNames="disease_id, descent_descent_of_disease"
constraintName="PRIMARY"
tableName="DISEASE_DESCENTS"/>
</changeSet>
<changeSet author="newbie" id="1449941926339-5">
<addPrimaryKey columnNames="descent_of_disease"
constraintName="PRIMARY"
tableName="ENUM_DESCENT"/>
</changeSet>
以及日志样本:
[DEBUG] liquibase - classpath:config/liquibase/master.xml: config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-4::newbie: Executing EXECUTE database command: ALTER TABLE PUBLIC.DISEASE_DESCENTS ADD CONSTRAINT "PRIMARY" PRIMARY KEY (disease_id, descent_descent_of_disease)
[INFO] liquibase - classpath:config/liquibase/master.xml: config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-4::newbie: Primary key added to DISEASE_DESCENTS (disease_id, descent_descent_of_disease)
[INFO] liquibase - classpath:config/liquibase/master.xml: config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-4::newbie: ChangeSet config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-4::newbie ran successfully in 1ms
[DEBUG] liquibase - classpath:config/liquibase/master.xml: config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-4::newbie: Computed checksum for addPrimaryKey:[
columnNames="disease_id, descent_descent_of_disease"
constraintName="PRIMARY"
tableName="DISEASE_DESCENTS"
] as eb8eba06bfcf2a30a029b8e49ea7d549
[DEBUG] liquibase - classpath:config/liquibase/master.xml: config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-4::newbie: Computed checksum for 7:eb8eba06bfcf2a30a029b8e49ea7d549: as 212a7c01d41bbcb1f9772a2d93161529
[DEBUG] liquibase - classpath:config/liquibase/master.xml: config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-4::newbie: Executing EXECUTE database command: INSERT INTO PUBLIC.DATABASECHANGELOG (ID, AUTHOR, FILENAME, DATEEXECUTED, ORDEREXECUTED, MD5SUM, DESCRIPTION, COMMENTS, EXECTYPE, LIQUIBASE) VALUES ('1449941926339-4', 'newbie', 'config/liquibase/changelog/20151115220815_added_entity_Descent.xml', NOW(), 6, '7:212a7c01d41bbcb1f9772a2d93161529', 'addPrimaryKey', '', 'EXECUTED', '3.3.2')
[DEBUG] liquibase - classpath:config/liquibase/master.xml: config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-4::newbie: Computed checksum for addPrimaryKey:[
columnNames="disease_id, descent_descent_of_disease"
constraintName="PRIMARY"
tableName="DISEASE_DESCENTS"
] as eb8eba06bfcf2a30a029b8e49ea7d549
[DEBUG] liquibase - classpath:config/liquibase/master.xml: config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-4::newbie: Computed checksum for 7:eb8eba06bfcf2a30a029b8e49ea7d549: as 212a7c01d41bbcb1f9772a2d93161529
[DEBUG] liquibase - classpath:config/liquibase/master.xml: config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-5::newbie: Running Changeset:config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-5::newbie
[DEBUG] liquibase - classpath:config/liquibase/master.xml: config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-5::newbie: Changeset config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-5::newbie
[DEBUG] liquibase - classpath:config/liquibase/master.xml: config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-5::newbie: Reading ChangeSet: config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-5::newbie
[DEBUG] liquibase - classpath:config/liquibase/master.xml: config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-5::newbie: Executing Statement: liquibase.statement.core.AddPrimaryKeyStatement@7334b84d
[DEBUG] liquibase - classpath:config/liquibase/master.xml: config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-5::newbie: Executing EXECUTE database command: ALTER TABLE PUBLIC.ENUM_DESCENT ADD CONSTRAINT "PRIMARY" PRIMARY KEY (descent_of_disease)
[ERROR] liquibase - classpath:config/liquibase/master.xml: config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-5::newbie: Change Set config/liquibase/changelog/20151115220815_added_entity_Descent.xml::1449941926339-5::newbie failed. Error: org.h2.jdbc.JdbcSQLException: Ograniczenie "PRIMARY" już istnieje
Constraint "PRIMARY" already exists; SQL statement: ALTER TABLE PUBLIC.ENUM_DESCENT ADD CONSTRAINT "PRIMARY" PRIMARY KEY (descent_of_disease) [90045-187]
liquibase.exception.DatabaseException: org.h2.jdbc.JdbcSQLException: Ograniczenie "PRIMARY" już istnieje
Constraint "PRIMARY" already exists; SQL statement: ALTER TABLE PUBLIC.ENUM_DESCENT ADD CONSTRAINT "PRIMARY" PRIMARY KEY (descent_of_disease) [90045-187] at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:316) ~[liquibase-core-3.3.2.jar:na] at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:55) ~[liquibase-core-3.3.2.jar:na] at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:122) ~[liquibase-core-3.3.2.jar:na]
at liquibase.database.AbstractJdbcDatabase.execute(AbstractJdbcDatabase.java:1227) ~[liquibase-core-3.3.2.jar:na]
at liquibase.database.AbstractJdbcDatabase.executeStatements(AbstractJdbcDatabase.java:1210) ~[liquibase-core-3.3.2.jar:na]
at liquibase.changelog.ChangeSet.execute(ChangeSet.java:550) ~[liquibase-core-3.3.2.jar:na]
at liquibase.changelog.visitor.UpdateVisitor.visit(UpdateVisitor.java:43) [liquibase-core-3.3.2.jar:na]
at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:73) [liquibase-core-3.3.2.jar:na]
at liquibase.Liquibase.update(Liquibase.java:200) [liquibase-core-3.3.2.jar:na]
at liquibase.integration.spring.SpringLiquibase.performUpdate(SpringLiquibase.java:353) [liquibase-core-3.3.2.jar:na]
at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:317) [liquibase-core-3.3.2.jar:na]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:634) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:444) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1119) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1014) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:299) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:129) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1477) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1222) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1120) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1044) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) [spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
解决方法:
<changeSet author="newbie" id="1449941926339-4">
<addPrimaryKey columnNames="disease_id, descent_descent_of_disease"
constraintName="PRIMARY_DISEASE_DESCENTS"
tableName="DISEASE_DESCENTS"/>
</changeSet>
<changeSet author="newbie" id="1449941926339-5">
<addPrimaryKey columnNames="descent_of_disease"
constraintName="PRIMARY_ENUM_DESCENT"
tableName="ENUM_DESCENT"/>
</changeSet>
MySQL: Creating table with two foreign keys fails with “Duplicate key name” error
【问题讨论】:
请不要在您的标题中添加标签。无需在标题中添加“[已解决]”。 您已将以下答案标记为已接受。这标志着问题已经解决。 几秒钟前我得到了我应该做的建议。 【参考方案1】:如果在创建表后添加主键,liquibase 将使用ALTER TABLE... ADD CONSTRAINT...
。并且您不能有多个具有相同名称的约束。所以H2报错。
您已经找到了解决方法。
也许另一种方法是将您的主键定义添加到 create table 语句:
<changeSet author="newbie" id="1449941926339-1">
<createTable tableName="ENUM_DESCENT">
<column name="descent_of_disease" type="VARCHAR(255)">
<constraints nullable="false"/>
<constraints primaryKey="true"/>
</column>
</createTable>
</changeSet>
然后主键定义将在CREATE TABLE...
语句中处理。
(顺便说一句:这不会发生在 mySQL 上,因为 mySQL 能够在 ALTER TABLE...
语句上使用 PRIMARY KEY
。因此,使用 mySQL DB liquibase 会将您的 addPrimaryKey
变更集转换为 ALTER TABLE... ADD PRIMARY KEY...
之类的东西)
【讨论】:
谢谢!或更简单的<constraints primaryKey="true" nullable="false"/>
以上是关于Liquibase 主键在 H2 上创建了两次的主要内容,如果未能解决你的问题,请参考以下文章
生成 forIndexName 的 Liquibase 差异在 h2 上不起作用
Liquibase:使用带 H2 数据库的 modifyDataType 重构将 INT 自动增量列更改为 BIGINT