gpt4 book ai didi

hibernate - Liquibase:MySqlSyntaxErrorException:表已存在

转载 作者:行者123 更新时间:2023-12-03 06:52:27 32 4
gpt4 key购买 nike

我看过几个相关的问题及其答案,但这对我的案子没有帮助。

我有一个 Spring boot 应用程序,我在其中使用 Jhipster 创建了实体,另外我自己添加了一些手动变更日志变更集。

我有一个医生实体,其中包含与“专业”的一对多单向关系,其中“医生”是该关系的所有者。因此,我指定了一个连接表来存储 Doctor 和 Specialty 外键。

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(
name="T_DOCTOR_SPECIALTY",
joinColumns = @JoinColumn( name="doctor_id"),
inverseJoinColumns = @JoinColumn( name="specialty_id")
)
private List<Specialty> specialties = new ArrayList<>();

为此,我添加了一个更改日志文件,其中甚至包含忽略特殊功能(如果已存在)的标记,

<changeSet id="20150415081455" author="waqas">
<preConditions onFail="MARK_RAN">
<not>
<tableExists tableName="T_DOCTOR_SPECIALTY"/>
</not>
</preConditions>
</changeSet>

<changeSet id="20150415081221" author="waqas">

<createTable tableName="T_DOCTOR_SPECIALTY">

<column name="id" type="bigint" autoIncrement="true">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="doctor_id" type="bigint">
<constraints nullable="false" />
</column>
<column name="specialty_id" type="bigint">
<constraints nullable="false" />
</column>
</createTable>

<addForeignKeyConstraint baseColumnNames="doctor_id"
baseTableName="T_DOCTOR_SPECIALTY"
constraintName="fk_doctor_id"
referencedColumnNames="id"
referencedTableName="T_DOCTOR"/>

<addForeignKeyConstraint baseColumnNames="specialty_id"
baseTableName="T_DOCTOR_SPECIALTY"
constraintName="fk_specialty_id"
referencedColumnNames="id"
referencedTableName="T_SPECIALTY"/>
</changeSet>

然后,我创建一个没有表的新数据库并运行该应用程序,但出现以下异常:

(Error creating bean with name 'delegatingApplicationListener' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cache.annotation.ProxyCachingConfiguration': Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' is defined)
[ERROR] org.springframework.boot.SpringApplication - Application startup failed
org.springframework.beans.factory.BeanCreationException:Error creating bean with name 'liquibase' defined in class path resource [io/aurora/ams/config/DatabaseConfiguration.class]: Invocation of init method failed; nested exception is liquibase.exception.MigrationFailedException: Migration failed for change set classpath:config/liquibase/changelog/20150415081221_added_entity_Doctor_Specialty.xml::20150415081221::waqas:
Reason: liquibase.exception.DatabaseException: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 't_doctor_specialty' already exists
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) ~[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.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956) ~[spring-context-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:747) ~[spring-context-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) ~[spring-context-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.2.3.RELEASE.jar:1.2.3.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686) ~[spring-boot-1.2.3.RELEASE.jar:1.2.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320) ~[spring-boot-1.2.3.RELEASE.jar:1.2.3.RELEASE]
at io.aurora.ams.Application.main(Application.java:64) [bin/:na]

Caused by: liquibase.exception.MigrationFailedException: Migration failed for change set classpath:config/liquibase/changelog/20150415081221_added_entity_Doctor_Specialty.xml::20150415081221::waqas:
Reason: liquibase.exception.DatabaseException: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 't_doctor_specialty' already exists
at liquibase.changelog.ChangeSet.execute(ChangeSet.java:586) ~[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]

Caused by: liquibase.exception.DatabaseException: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 't_doctor_specialty' already exists
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]

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 't_doctor_specialty' already exists
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_40]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_40]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_40]
at java.lang.reflect.Constructor.newInstance(Constructor.java:422) ~[na:1.8.0_40]
at com.mysql.jdbc.Util.handleNewInstance(Util.java:377) ~[mysql-connector-java-5.1.34.jar:5.1.34]
at com.mysql.jdbc.Util.getInstance(Util.java:360) ~[mysql-connector-java-5.1.34.jar:5.1.34]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:978) ~[mysql-connector-java-5.1.34.jar:5.1.34]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3887) ~[mysql-connector-java-5.1.34.jar:5.1.34]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3823) ~[mysql-connector-java-5.1.34.jar:5.1.34]
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435) ~[mysql-connector-java-5.1.34.jar:5.1.34]
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582) ~[mysql-connector-java-5.1.34.jar:5.1.34]
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2526) ~[mysql-connector-java-5.1.34.jar:5.1.34]
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2484) ~[mysql-connector-java-5.1.34.jar:5.1.34]
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:848) ~[mysql-connector-java-5.1.34.jar:5.1.34]
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:742) ~[mysql-connector-java-5.1.34.jar:5.1.34]
at com.zaxxer.hikari.proxy.StatementProxy.execute(StatementProxy.java:83) ~[HikariCP-2.2.5.jar:na]
at com.zaxxer.hikari.proxy.StatementJavassistProxy.execute(StatementJavassistProxy.java) ~[HikariCP-2.2.5.jar:na]
at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:314) ~[liquibase-core-3.3.2.jar:na]

我想提一下,这种关系仅在 20150415081221_added_entity_Doctor_Specialty.xml 变更日志中提到,而不是在 Doctor 中。

我进行了很多搜索,但不确定是什么原因造成的。任何帮助将不胜感激。

最佳答案

虽然我仍然不确定 Liquibase 关于此错误的确切问题是什么,但由于 Liquibase 文档中提到的限制,使用 preCondition 标记的解决方法不起作用:

“在变更集之外(例如,在变更日志的开头),只有 HALT 和 WARN 是可能的值。”

我缺少将前置条件放在 createTable 之前的 ChangeSet 中。

<changeSet id="20150415081221" author="waqas">
<preConditions onFail="MARK_RAN">
<not>
<tableExists tableName="T_DOCTOR_SPECIALTY"/>
</not>
</preConditions>
<createTable tableName="T_DOCTOR_SPECIALTY">...

关于hibernate - Liquibase:MySqlSyntaxErrorException:表已存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29667760/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com