添加外键时发生liquibase错误

gkn4icbw  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(438)

我正在使用jhipster开发springboot和angularjs。jhipster正在使用liquibase创建表、加载数据、更新e.t.c
我正在使用mysql数据库。
创建table 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>

另一张table: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)

你能帮我把我做错了什么吗。

n6lpvg4x

n6lpvg4x1#

我先创作电视节目然后再创作流派。。所以是打错了。。更改了更改日志顺序,工作正常

相关问题