简体   繁体   中英

Why do I get “Referential integrity constraint violation” with jpaDerivedIdentifier

After using Jhipster to generate app from jdl file, I got error below at app startup. I only got this issue when I used jpaDerivedIdentifier on the OneToOne relationship to user table.

liquibase.exception.MigrationFailedException: Migration failed for change set config/liquibase/changelog/20201128181606_added_entity_constraints_RegistrationOrder.xml::20201128181606-2::jhipster:
     Reason: liquibase.exception.DatabaseException: Referential integrity constraint violation: "FK_REGISTRATION_ORDER_STUDENT_ID: PUBLIC.REGISTRATION_ORDER FOREIGN KEY(STUDENT_ID) REFERENCES PUBLIC.STUDENT(ID)"; SQL statement:
ALTER TABLE PUBLIC.registration_order ADD CONSTRAINT fk_registration_order_student_id FOREIGN KEY (student_id) REFERENCES PUBLIC.student (id) [23506-200] [Failed SQL: (23506) ALTER TABLE PUBLIC.registration_order ADD CONSTRAINT fk_registration_order_student_id FOREIGN KEY (student_id) REFERENCES PUBLIC.student (id)]
        at liquibase.changelog.ChangeSet.execute(ChangeSet.java:646)
        at liquibase.changelog.visitor.UpdateVisitor.visit(UpdateVisitor.java:53)
        at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:83)
        at liquibase.Liquibase.update(Liquibase.java:202)
        at liquibase.Liquibase.update(Liquibase.java:179)
        at liquibase.integration.spring.SpringLiquibase.performUpdate(SpringLiquibase.java:366)
        at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:314)
        at org.springframework.boot.autoconfigure.liquibase.DataSourceClosingSpringLiquibase.afterPropertiesSet(DataSourceClosingSpringLiquibase.java:46)
        at io.github.jhipster.config.liquibase.AsyncSpringLiquibase.initDb(AsyncSpringLiquibase.java:118)
        at io.github.jhipster.config.liquibase.AsyncSpringLiquibase.lambda$afterPropertiesSet$0(AsyncSpringLiquibase.java:93)
        at io.github.jhipster.async.ExceptionHandlingAsyncTaskExecutor.lambda$createWrappedRunnable$1(ExceptionHandlingAsyncTaskExecutor.java:78)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
        at java.base/java.lang.Thread.run(Thread.java:834)

My JDL is:

entity Student {
    birthDate LocalDate
    nickName String maxlength(50)
}

entity RegistrationOrder{
    paymentAmount BigDecimal required min(0)
    materialFee BigDecimal required min(0)
    placedDate Instant required
}

relationship OneToOne {
    Student{user(login) required} to User with jpaDerivedIdentifier
}

relationship ManyToOne {
    RegistrationOrder{student required} to Student
}

If I removed "with jpaDerivedIdentifier", the Jhipster app started without error. Any idea?

Probably because there are already some data loaded from CSV by Liquibase that violate the foreign key you add in your migration. Either change the data in the CSV files or change the order of migrations.

The fact that you're sharing an id with User does not help of course because the User table is created and loaded in first migration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM