user6250770
user6250770

Reputation: 690

Liquibase error while adding foreign keys

I am using Jhipster and developing SpringBoot and AngularJS. Jhipster is using liquibase for creating the table, loading data, update e.t.c

I am using Mysql Database.

creating 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>

creating CategoryStudio table

<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>

Constraints:

<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>

Creating another table "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>

Contsraints:

    <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>

another 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>

If i am adding only one foriegn key then it's working fine.But, when i added second foriegn key, it's throing the below error.

my equation is , each category have many tv shows and each tvshows have many Genres

i am getting the error as:

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)

Can you please help me out what i am doing wrong here.

Upvotes: 1

Views: 4137

Answers (1)

user6250770
user6250770

Reputation: 690

I was creating db tvshow first and then genres .. so it was hitting error .. changed the change log sequence and worked fine

Upvotes: 3

Related Questions