Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Project bugs: Liquibase constraint violation with double relations and Tests errors #9563

Closed
1 task done
Gabrui opened this issue Apr 15, 2019 · 11 comments · Fixed by #9638
Closed
1 task done
Milestone

Comments

@Gabrui
Copy link
Contributor

Gabrui commented Apr 15, 2019

Overview of the issue

There are two bugs related when I create a new project:

  • Liquibase constraint violations (seems to be related to the jpaDerivedIdentifier)
2019-04-15 09:44:28.714 ERROR 15838 --- [-3-teste-task-1] liquibase.changelog.ChangeSet            : Change Set config/liquibase/changelog/20190415124316_added_entity_constraints_MyEntityComplex.xml::20190415124316-2::jhipster failed.  Error: Referential integrity constraint violation: "FK_MY_ENTITY_COMPLEX_MY_ENTITY_USER_EXTRA_ID: PUBLIC.MY_ENTITY_COMPLEX FOREIGN KEY(MY_ENTITY_USER_EXTRA_ID) REFERENCES PUBLIC.MY_ENTITY_USER_EXTRA(ID)"; SQL statement:
ALTER TABLE PUBLIC.my_entity_complex ADD CONSTRAINT fk_my_entity_complex_my_entity_user_extra_id FOREIGN KEY (my_entity_user_extra_id) REFERENCES PUBLIC.my_entity_user_extra (id) [23506-199] [Failed SQL: ALTER TABLE PUBLIC.my_entity_complex ADD CONSTRAINT fk_my_entity_complex_my_entity_user_extra_id FOREIGN KEY (my_entity_user_extra_id) REFERENCES PUBLIC.my_entity_user_extra (id)]
2019-04-15 09:44:28.717 ERROR 15838 --- [-3-teste-task-1] i.g.j.c.liquibase.AsyncSpringLiquibase   : Liquibase could not start correctly, your database is NOT ready: Migration failed for change set config/liquibase/changelog/20190415124316_added_entity_constraints_MyEntityComplex.xml::20190415124316-2::jhipster:
     Reason: liquibase.exception.DatabaseException: Referential integrity constraint violation: "FK_MY_ENTITY_COMPLEX_MY_ENTITY_USER_EXTRA_ID: PUBLIC.MY_ENTITY_COMPLEX FOREIGN KEY(MY_ENTITY_USER_EXTRA_ID) REFERENCES PUBLIC.MY_ENTITY_USER_EXTRA(ID)"; SQL statement:
ALTER TABLE PUBLIC.my_entity_complex ADD CONSTRAINT fk_my_entity_complex_my_entity_user_extra_id FOREIGN KEY (my_entity_user_extra_id) REFERENCES PUBLIC.my_entity_user_extra (id) [23506-199] [Failed SQL: ALTER TABLE PUBLIC.my_entity_complex ADD CONSTRAINT fk_my_entity_complex_my_entity_user_extra_id FOREIGN KEY (my_entity_user_extra_id) REFERENCES PUBLIC.my_entity_user_extra (id)]

liquibase.exception.MigrationFailedException: Migration failed for change set config/liquibase/changelog/20190415124316_added_entity_constraints_MyEntityComplex.xml::20190415124316-2::jhipster:
     Reason: liquibase.exception.DatabaseException: Referential integrity constraint violation: "FK_MY_ENTITY_COMPLEX_MY_ENTITY_USER_EXTRA_ID: PUBLIC.MY_ENTITY_COMPLEX FOREIGN KEY(MY_ENTITY_USER_EXTRA_ID) REFERENCES PUBLIC.MY_ENTITY_USER_EXTRA(ID)"; SQL statement:
ALTER TABLE PUBLIC.my_entity_complex ADD CONSTRAINT fk_my_entity_complex_my_entity_user_extra_id FOREIGN KEY (my_entity_user_extra_id) REFERENCES PUBLIC.my_entity_user_extra (id) [23506-199] [Failed SQL: ALTER TABLE PUBLIC.my_entity_complex ADD CONSTRAINT fk_my_entity_complex_my_entity_user_extra_id FOREIGN KEY (my_entity_user_extra_id) REFERENCES PUBLIC.my_entity_user_extra (id)]
  • Test with errors:
[ERROR] Errors: 
[ERROR]   MyEntityComplexResourceIT.getAllMyEntityComplexesByMyEntityOneIsEqualToSomething:329 » Persistence
[ERROR]   MyEntityComplexResourceIT.getAllMyEntityComplexesByMyEntityUserExtraIsEqualToSomething:346 » Persistence
[ERROR]   MyEntityUserExtraResourceIT.getAllMyEntityUserExtrasByMyEntityThreeIsEqualToSomething:419 » Persistence

Motivation for or Use Case

Creating a new project with an entity that has filter and pagination creates broken test. Although it is easy to fix the test I didn't expect this error to happen and it may scare new users.

Creating a new project with an entity that has jpaDerivedIdentifier and is referenced by another in a ManyToOne relation.

Reproduce the error

You should created a new project, import the JDL and run the ./mvnw clean install. The default tests fail because it tries to save the same object twice. (BUG2) This issue is present even in 5.8.2 and 6.0.0-beta0.

To reproduce the liquibase error, I did a ./mvnw clean install -Dmaven.test.skip=true, and then a ./mvnw. This issue is present only in 6.0.0-beta0 and I couldn't figure the cause of it.

Suggest a Fix

There isn't any Liquibase error without the jpaDerivedIdentifier. I supposed it is related to the data that was automatically generated and loaded. EDIT: It is the my_entity_complex.csv that references users that doesn't exist (ID 1 up to 10). Replacing the ID larger than 4 solves the problem.

To fix the test error in my code I just did substitute

MyEntityOne myEntityOne = MyEntityOneResourceIT.createEntity(em);
em.persist(myEntityOne);
em.flush();
myEntityComplex.setMyEntityOne(myEntityOne);

to

MyEntityOne myEntityOne = myEntityComplex.getMyEntityOne();

It isn't necessary to persist the entity in this case because the entity is already persisted in the "public static MyEntityComplex createEntity" method. The exception is caused by the unique constraint.

JHipster Version(s)

6.0.0-beta.0 (but the test error also happens on the 5.8.2, the Liquibase error is new)

jh-3-teste@0.0.0 /home/gabriel/Projetos/jh3teste
└── generator-jhipster@6.0.0-beta.0 

JHipster configuration, a .yo-rc.json file generated in the root folder
.yo-rc.json file
{
  "generator-jhipster": {
    "promptValues": {
      "packageName": "org.learn.testing",
      "nativeLanguage": "pt-br"
    },
    "jhipsterVersion": "6.0.0-beta.0",
    "applicationType": "monolith",
    "baseName": "jh3teste",
    "packageName": "org.learn.testing",
    "packageFolder": "org/learn/testing",
    "serverPort": "8080",
    "authenticationType": "jwt",
    "cacheProvider": "ehcache",
    "enableHibernateCache": true,
    "websocket": "spring-websocket",
    "databaseType": "sql",
    "devDatabaseType": "h2Disk",
    "prodDatabaseType": "postgresql",
    "searchEngine": "elasticsearch",
    "messageBroker": false,
    "serviceDiscoveryType": false,
    "buildTool": "maven",
    "enableSwaggerCodegen": false,
    "clientFramework": "angularX",
    "clientTheme": "materia",
    "clientThemeVariant": "primary",
    "useSass": true,
    "clientPackageManager": "yarn",
    "testFrameworks": ["protractor"],
    "jhiPrefix": "jhi",
    "entitySuffix": "",
    "dtoSuffix": "DTO",
    "otherModules": [],
    "enableTranslation": true,
    "nativeLanguage": "pt-br",
    "languages": ["pt-br", "en", "es"]
  }
}
JDL for the Entity configuration(s) entityName.json files generated in the .jhipster directory
JDL entity definitions
entity MyEntityOne {
  name String required unique,
  value Integer required
}
entity MyEntityUserExtra {
  name String required unique,
  departure LocalDate required
}
entity MyEntityThree {
  name String required unique,
  desc String required
}
entity MyEntityComplex {
  name String required unique,
  desc String required
}
relationship OneToOne {
  MyEntityUserExtra{user(login)} to User with jpaDerivedIdentifier
}
relationship ManyToOne {
  MyEntityUserExtra{myEntityThree(name) required} to MyEntityThree,
  MyEntityComplex{myEntityOne(name) required} to MyEntityOne,
  MyEntityComplex{myEntityUserExtra(name) required} to MyEntityUserExtra
}

paginate MyEntityOne, MyEntityUserExtra, MyEntityThree, MyEntityComplex with pagination
service MyEntityOne, MyEntityUserExtra, MyEntityThree, MyEntityComplex with serviceImpl
filter MyEntityOne, MyEntityUserExtra, MyEntityThree, MyEntityComplex

Environment and Tools

openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)

git version 2.11.0

node: v10.15.3

npm: 6.4.1

yeoman: 2.0.5

yarn: 1.15.2

Docker version 18.09.5, build e8ff056dbc

docker-compose version 1.24.0, build 0aa59064

Browsers and Operating System

Using Debian 9.8. Generated the project with --yarn.

  • Checking this box is mandatory (this is just to show you read everything)
@Gabrui Gabrui changed the title New Project bugs: Liquibase constraint violation with double relations and Tests errors New Project bugs: Liquibase constraint violation with double relations and Tests errors bug 🐛 Apr 15, 2019
@Gabrui Gabrui changed the title New Project bugs: Liquibase constraint violation with double relations and Tests errors bug 🐛 New Project bugs: Liquibase constraint violation with double relations and Tests errors Apr 15, 2019
@pmverma
Copy link
Member

pmverma commented Apr 15, 2019

Thanks @Gabrui for reporting the issue. The code which you replaced was added for required entity. To reproduce the error, I am not seeing the with jpaDerivedIdentifier in your JDL file. Can you update your JDL entity definitions?

@Gabrui How did you generated the JDL entity definitions? With jhipster info or jhipster export-jdl?
@MathieuAA I think jhipster info or jhipster export-jdl not producing with jpaDerivedIdentifier in JDL entity definitions.

@Gabrui
Copy link
Contributor Author

Gabrui commented Apr 15, 2019

How did you generated the JDL entity definitions?

I used jhipster info.
It seems that jhipster info doesn't show the jpaDerivedIdentifier. Another issue here (I think). Also I have just tested the jhipster export-jdl and it doesn't print the jpaDerivedIdentifier either.

Can you update your JDL entity definitions?

Done, I have updated the JDL in my comment.

@pmverma
Copy link
Member

pmverma commented Apr 15, 2019

@Gabrui, I gave a quick try by removing the with jpaDerivedIdentifier and the tests are still failing.
Can you try with following to confirm and create an another issue if you think it is strange?

entity MyEntityOne {
  name String required unique,
  value Integer required
}
entity MyEntityUserExtra {
  name String required unique,
  departure LocalDate required
}
entity MyEntityThree {
  name String required unique,
  desc String required
}
entity MyEntityComplex {
  name String required unique,
  desc String required
}
relationship OneToOne {
  MyEntityUserExtra{user(login)} to User //with jpaDerivedIdentifier
}
relationship ManyToOne {
  MyEntityUserExtra{myEntityThree(name) required} to MyEntityThree,
  MyEntityComplex{myEntityOne(name) required} to MyEntityOne,
  MyEntityComplex{myEntityUserExtra(name) required} to MyEntityUserExtra
}

paginate MyEntityOne, MyEntityUserExtra, MyEntityThree, MyEntityComplex with pagination
service MyEntityOne, MyEntityUserExtra, MyEntityThree, MyEntityComplex with serviceImpl
filter MyEntityOne, MyEntityUserExtra, MyEntityThree, MyEntityComplex

  1. jhipster import-jdl xxxxxx.jdl --force
  2. ./mvnw verify

@Gabrui
Copy link
Contributor Author

Gabrui commented Apr 15, 2019

Yes, the tests fails because of the paginate and filter directives. (It tries to save a unique value twice in the tests)
But now the application can launch (./mvnw), skipping the tests, because there is no constraint violation.
These are two different bugs that I found using that JDL.

@pmverma
Copy link
Member

pmverma commented Apr 15, 2019

Can you create a new issue for that if it is/was not reported so that it can be tracked and fixed?

@Gabrui
Copy link
Contributor Author

Gabrui commented Apr 16, 2019

Can you create a new issue for that if it is/was not reported so that it can be tracked and fixed?

I have reported those 2 separated bugs here in this issue. Is it advisable to create another issue to address each of those bugs separately?

@MathieuAA
Copy link
Member

If there's a bug in the JDL parsing/exporting, please create an issue in JHipster-Core for that

@Gabrui
Copy link
Contributor Author

Gabrui commented Apr 17, 2019

Thanks, I have reported the export bug to the JHipster-Core #319

@pmverma
Copy link
Member

pmverma commented Apr 17, 2019

@Gabrui The liquibase error is coming from my_entity_complex.csv because my_entity_user_extra.csv has only 4 records. Remember this is only used in dev mode so don't worry.
I will see how this can be improved.

And yes, it is better to create separate issues so that they can be tracked and fixed independently, keeping clear the matters.

@Gabrui
Copy link
Contributor Author

Gabrui commented Apr 22, 2019

I just realized that there is another error in the tests, related to a elastic search verification, introduced in #9094, @pmverma

The Elastic Search Service is called twice, verify(mock<%= entityClass %>SearchRepository, times(0)).save(<%= asEntity(entityInstance) %>) as the elastic search isn't detached by em.detach(), (so it is updated when the user is changed) and also with the put request.

[ERROR] Failures: 
[ERROR]   MyEntityUserExtraResourceIT.updateMyEntityUserExtraMapsIdAssociationWithNewId:220 
myEntityUserExtraSearchRepository bean.save(
    MyEntityUserExtra{id=1342, name='AAAAAAAAAA', departure='1970-01-01'}
);
Never wanted here:
-> at com.mycompany.myapp.web.rest.MyEntityUserExtraResourceIT.updateMyEntityUserExtraMapsIdAssociationWithNewId(MyEntityUserExtraResourceIT.java:220)
But invoked here:
-> at com.mycompany.myapp.service.impl.MyEntityUserExtraServiceImpl.save(MyEntityUserExtraServiceImpl.java:53)
-> at com.mycompany.myapp.service.impl.MyEntityUserExtraServiceImpl.save(MyEntityUserExtraServiceImpl.java:53)

@pmverma
Copy link
Member

pmverma commented Apr 23, 2019

@Gabrui thanks for your comments. Actually the time value must be 2. I have noted it to do PR one I come back from vacation.

What I do not know is why the automated build passed. I will see that too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants