Hello everyone. I have just completed initialization of OpenMRS core on PostgreSQL. All changesets in core are executing after some changes. I have added the legacy UI module and was trying to create a patient where I got the error
Caused by: org.postgresql.util.PSQLException: ERROR: relation "hibernate_sequence" does not exist
Looking at postgresql database, it appears that there is a sequence with name
patient_identifier_patient_identifier_id_seq but it has not been linked to patient_id.
Is there a reason for this or am I missing something?
We donât let Hibernate create tables, etc. by default. We use Liquibase to setup the database, etc. It maybe be necessary to add a Liquibase changeset to setup the Hibernate sequence.
Thanks for the info @ibacher . I added the required changeset and the sequence problem resolved but now there are some other issues I am bumping into. Let me see if can resolve it or else Iâll be back with some more questions.
Thanks @ibacher and @mksd . I am now able to add patients successfully to PostgreSQL. I will now be heading to the admininstration section and verify that the different functionalities are working correctly.
If there is anything I should keep in mind while testing openmrs-core on PostgreSQL, please do suggest.
@ibacher@mksd , creating hibernate_sequence with start values as 1 leads to primary key violation error when you add patient for first time because there is already a person with id = 1 (superuser) . You move 1 page back and again try to add patient, and patient gets added successfully since the sequence autoincremented. There appears to be a conflict between sequence defined for person and default fallback sequence hibernate_squence that hibernate uses.
Making the hibernate_sequence start from 2 solves the issue. But is this the right way to get over this problem ?
If you have not created a sequence specifically for the object, then hibernate looks for its default sequence which is hibernate_sequence and uses it even if the objects may be different. In OpenMRS there are sequences that have been created specifically for objects( example Person ) but its not the case with Patient.
Working on this problem for quite sometime, I have observed that although the different sequences for different entities were being created but none of them were being utilized, all the different entities were making use of hibernate_sequence for persistance which is the default hibernate fallback sequence.
Searching for this problem I have finally found that starting from hibernate 5+ we need to change all the mapping of sequence generators in hibernate files to
That is change âsequenceâ to âsequence_nameâ .
Doing this for Person need not required the use of hibernate_sequence for Patient since it was just extending the Person class and Person Id was automatically made equal to Patient Id when persisting making use of person_person_id_seq.
And now finally the sequence problem is resolved while saving Patient in PostgreSQL. I am not able to figure out why this was not a problem in MySQL though it just makes use of autoincrement.
The reason it isnât an issue for MySQL, is that MySQL allows columns to be defined as have an AUTO_INCREMENT attribute. Years ago, PostgreSQL did not support this functionality and when they did add this (in the form of the SERIAL datatype) it was just as a thin wrapper around sequences. The upshot is that Hibernate has always been able to rely on MySQLâs AUTO_INCREMENT functionality, but hasnât for PostgreSQL.
Incidentally, you may want to experiment with just making the primary keys âSERIALâ types so that we donât have to mess too much with identity generation.