添加外键时出现Liquibase错误

时间:2018-08-20 14:30:44

标签: mysql foreign-keys jhipster liquibase

我正在使用Jhipster并开发SpringBoot和AngularJS。 Jhipster正在使用liquibase创建表,加载数据,更新e.t.c

我正在使用Mysql数据库。

创建表格Studio

    <changeSet id="00000000000002" author="Mehbub"> <createTable tableName="studioname"> 
<column name="studioname_id" type="bigint" autoIncrement="${autoIncrement}"> 
<constraints primaryKey="true" nullable="false"/>
 </column>
 <column name="studioname_name" type="varchar(50)"> 
<constraints unique="true" nullable="false" />
 </column> </createTable>
 </changeSet>

创建CategoryStudio表

<changeSet id="00000000000002" author="admin">
        <createTable tableName="categorystudio">
            <column name="categorystudio_id" type="bigint" autoIncrement="${autoIncrement}">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="categorystudio_title" type="varchar(50)">
                <constraints unique="true" nullable="false" />
            </column>

            <column name="studio_id" type="bigint">
                <constraints nullable="false" />
            </column>

        </createTable> </<changeSet>

约束:

<changeSet id="00000000000002-1" author="Mehbub">
        <addForeignKeyConstraint baseColumnNames="studio_id"
                                 baseTableName="categorystudio"
                                 constraintName="fk_studioname_id"
                                 referencedColumnNames="studioname_id"
                                 referencedTableName="studioname"
                                 deleteCascade="true"
                                 onDelete="CASCADE"/>
    </changeSet>

创建另一个表“ tvshow

<changeSet id="00" author="Mehbub">
    <createTable tableName="tvshow">
        <column name="tvshow_id" type="bigint" autoIncrement="${autoIncrement}">
            <constraints primaryKey="true" nullable="false"/>
        </column>
        <column name="tvshow_name" type="varchar(50)">
            <constraints unique="true" nullable="false" />
        </column>
        <column name="movieCategory_id" type="bigint">
            <constraints nullable="false" />
        </column>

        <column name="genreCategory_id" type="bigint">
            <constraints nullable="false" />
        </column>
    </createTable>
</changeSet>

约束:

    <addForeignKeyConstraint baseColumnNames="movieCategory_id"
                             baseTableName="tvshow"
                             constraintName="fk_tvshow_movieCategory_id"
                             referencedColumnNames="categorystudio_id"
                             referencedTableName="categorystudio"
                             deleteCascade="true"
                             onDelete="CASCADE"/>

    <addForeignKeyConstraint baseColumnNames="genreCategory_id"
                             baseTableName="tvshow"
                             constraintName="fk_tvshow_genreCategory_id"
                             referencedColumnNames="genresCategory_id"
                             referencedTableName="genresCategory"
                             deleteCascade="true"
                             onDelete="CASCADE"/>
</changeSet>

另一个表格: genresCategory

<changeSet id="00" author="admin">
    <createTable tableName="genresCategory">
        <column name="genresCategory_id" type="bigint" autoIncrement="${autoIncrement}">
            <constraints primaryKey="true" nullable="false"/>
        </column>
        <column name="genresCategory_name" type="varchar(50)">
            <constraints unique="true" nullable="false" />
        </column>

    </createTable>
</changeSet>

如果我仅添加一个前键,则可以正常工作。但是,当我添加第二个前键时,则出现以下错误。

我的等式是,每个类别都有很多电视节目,每个电视节目都有很多流派

我收到以下错误消息:

liquibase.exception.MigrationFailedException: Migration failed for change set config/liquibase/changelog/00_constraints_tvShow.xml::00::Mehbub:
     Reason: liquibase.exception.DatabaseException: Can't create table `allcomiclibrary`.`#sql-2ad_3ac` (errno: 150 "Foreign key constraint is incorrectly formed") [Failed SQL: ALTER TABLE allComicLibrary.tvshow ADD CONSTRAINT fk_tvshow_genreCategory_id FOREIGN KEY (genreCategory_id) REFERENCES allComicLibrary.genresCategory (genresCategory_id) ON DELETE CASCADE]   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:103)   at io.github.jhipster.config.liquibase.AsyncSpringLiquibase.lambda$afterPropertiesSet$0(AsyncSpringLiquibase.java:83)   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)

您能帮我解决我在这里做错的事情吗?

1 个答案:

答案 0 :(得分:2)

我先创建db tvshow,然后创建流派..因此它遇到错误..更改了更改日志顺序并正常工作