我正在使用liquibase,spring-boot和hibernate处理应用程序。
使用的数据库是PostgreSQL。为了在启动时填充数据库,我在src / main / resources中配置了一个data.sql文件,其中包含一些insert语句。
此外,在启动之后还有liquibase尝试应用所有更改集,其中之一是使用data.sql文件填充的表的创建。因此,在执行mvn
cmd以启动应用程序时,我收到以下非阻塞错误:
2018-04-25 14:33:53.417 ERROR 11060 --- [neut-Executor-1] liquibase : classpath:config/liquibase/master.xml: config/liquibase/changelog/20180424154826_added_entity_Container.xml::20180424154826-1::jhipster: Change Set config/liquibase/changelog/20180424154826_added_entity_Container.xml::20180424154826-1::jhipster failed. Error: ERROR: relation "container" already exists [Failed SQL: CREATE TABLE public.container (id BIGINT NOT NULL, name VARCHAR(255) NOT NULL, description VARCHAR(2000), container_type VARCHAR(255), created TIMESTAMP WITHOUT TIME ZONE, CONSTRAINT PK_CONTAINER PRIMARY KEY (id))]
2018-04-25 14:33:53.453 ERROR 11060 --- [neut-Executor-1] i.g.j.c.liquibase.AsyncSpringLiquibase : Liquibase could not start correctly, your database is NOT ready: Migration failed for change set config/liquibase/changelog/20180424154826_added_entity_Container.xml::20180424154826-1::jhipster:
Reason: liquibase.exception.DatabaseException: ERROR: relation "container" already exists [Failed SQL: CREATE TABLE public.container (id BIGINT NOT NULL, name VARCHAR(255) NOT NULL, description VARCHAR(2000), container_type VARCHAR(255), created TIMESTAMP WITHOUT TIME ZONE, CONSTRAINT PK_CONTAINER PRIMARY KEY (id))]
liquibase.exception.MigrationFailedException: Migration failed for change set config/liquibase/changelog/20180424154826_added_entity_Container.xml::20180424154826-1::jhipster:
Reason: liquibase.exception.DatabaseException: ERROR: relation "container" already exists [Failed SQL: CREATE TABLE public.container (id BIGINT NOT NULL, name VARCHAR(255) NOT NULL, description VARCHAR(2000), container_type VARCHAR(255), created TIMESTAMP WITHOUT TIME ZONE, CONSTRAINT PK_CONTAINER PRIMARY KEY (id))]
at liquibase.changelog.ChangeSet.execute(ChangeSet.java:619)
at liquibase.changelog.visitor.UpdateVisitor.visit(UpdateVisitor.java:51)
at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:79)
at liquibase.Liquibase.update(Liquibase.java:214)
at liquibase.Liquibase.update(Liquibase.java:192)
at liquibase.integration.spring.SpringLiquibase.performUpdate(SpringLiquibase.java:431)
at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:388)
at io.github.jhipster.config.liquibase.AsyncSpringLiquibase.initDb(AsyncSpringLiquibase.java:94)
at io.github.jhipster.config.liquibase.AsyncSpringLiquibase.lambda$afterPropertiesSet$0(AsyncSpringLiquibase.java:77)
at io.github.jhipster.async.ExceptionHandlingAsyncTaskExecutor.lambda$createWrappedRunnable$1(ExceptionHandlingAsyncTaskExecutor.java:68)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: liquibase.exception.DatabaseException: ERROR: relation "container" already exists [Failed SQL: CREATE TABLE public.container (id BIGINT NOT NULL, name VARCHAR(255) NOT NULL, description VARCHAR(2000), container_type VARCHAR(255), created TIMESTAMP WITHOUT TIME ZONE, CONSTRAINT PK_CONTAINER PRIMARY KEY (id))]
at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:309)
at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:55)
at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:113)
at liquibase.database.AbstractJdbcDatabase.execute(AbstractJdbcDatabase.java:1277)
at liquibase.database.AbstractJdbcDatabase.executeStatements(AbstractJdbcDatabase.java:1259)
at liquibase.changelog.ChangeSet.execute(ChangeSet.java:582)
... 12 common frames omitted
Caused by: org.postgresql.util.PSQLException: ERROR: relation "container" already exists
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2455)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2155)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:288)
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:430)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:356)
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:303)
at org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:289)
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:266)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:262)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114)
at com.sun.proxy.$Proxy129.execute(Unknown Source)
at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:307)
... 17 common frames omitted
我想的是执行data.sql的hibernate会在liquibase迁移过程之前执行一段时间,所以也许应该有办法避免这种异常。是否可以在liquibase迁移后强制执行hibernate导入data.sql?
答案 0 :(得分:0)
您必须决定要执行更改的机制。因此,您可以使用hibernate创建表并插入数据,或者您可以使用liquibase同时创建表和数据。我在之前的项目中所做的是将hibenate设置为仅验证模式spring.jpa.hibernate.ddl-auto=validate
并使用liquibase创建表并插入数据。