Error starting module - EMR API Module

I am getting this error when I try to start the API Module. This error appears in several other modules after I upgraded them

Log entries

   INFO 1/25/21 11:52 AM:liquibase: Successfully released change log lock
25-01-2021 11:52:11 [WARN ] ModuleFactory - Error while trying to start module: addresshierarchy
org.openmrs.module.ModuleException: Unable to update data model using liquibase.xml. Module: Address Hierarchy
        at org.openmrs.module.ModuleFactory.runLiquibase(ModuleFactory.java:1072)
        at org.openmrs.module.ModuleFactory.startModuleInternal(ModuleFactory.java:768)
        at org.openmrs.api.context.Daemon$1.run(Daemon.java:74)
Caused by: liquibase.exception.MigrationFailedException: Migration failed for change set liquibase.xml::address_hierarchy-1595797202649-7::aman (generated):
     Reason: liquibase.exception.DatabaseException: Error executing SQL ALTER TABLE `address_hierarchy_entry` ADD CONSTRAINT `level_to_level` FOREIGN KEY (`level_id`) REFERENCES `address_hierarchy_level` (`address_hierarchy_level_id`) ON UPDATE NO ACTION ON DELETE NO ACTION: Cannot add or update a child row: a foreign key constraint fails (`openmrs`.`#sql-8a5_32`, CONSTRAINT `level_to_level` FOREIGN KEY (`level_id`) REFERENCES `address_hierarchy_level` (`address_hierarchy_level_id`) ON DELETE NO ACTION ON UPDATE NO ACTION):
          Caused By: Error executing SQL ALTER TABLE `address_hierarchy_entry` ADD CONSTRAINT `level_to_level` FOREIGN KEY (`level_id`) REFERENCES `address_hierarchy_level` (`address_hierarchy_level_id`) ON UPDATE NO ACTION ON DELETE NO ACTION: Cannot add or update a child row: a foreign key constraint fails (`openmrs`.`#sql-8a5_32`, CONSTRAINT `level_to_level` FOREIGN KEY (`level_id`) REFERENCES `address_hierarchy_level` (`address_hierarchy_level_id`) ON DELETE NO ACTION ON UPDATE NO ACTION):
          Caused By: Cannot add or update a child row: a foreign key constraint fails (`openmrs`.`#sql-8a5_32`, CONSTRAINT `level_to_level` FOREIGN KEY (`level_id`) REFERENCES `address_hierarchy_level` (`address_hierarchy_level_id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
        at liquibase.changelog.ChangeSet.execute(ChangeSet.java:347)
        at liquibase.changelog.visitor.UpdateVisitor.visit(UpdateVisitor.java:27)
        at org.openmrs.util.DatabaseUpdater$1OpenmrsUpdateVisitor.visit(DatabaseUpdater.java:189)
        at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:58)
        at org.openmrs.util.DatabaseUpdater.executeChangelog(DatabaseUpdater.java:218)
        at org.openmrs.module.ModuleFactory.runLiquibase(ModuleFactory.java:1062)
        ... 2 more
Caused by: liquibase.exception.DatabaseException: Error executing SQL ALTER TABLE `address_hierarchy_entry` ADD CONSTRAINT `level_to_level` FOREIGN KEY (`level_id`) REFERENCES `address_hierarchy_level` (`address_hierarchy_level_id`) ON UPDATE NO ACTION ON DELETE NO ACTION: Cannot add or update a child row: a foreign key constraint fails (`openmrs`.`#sql-8a5_32`, CONSTRAINT `level_to_level` FOREIGN KEY (`level_id`) REFERENCES `address_hierarchy_level` (`address_hierarchy_level_id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
        at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:62)
        at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:104)
        at liquibase.database.AbstractDatabase.execute(AbstractDatabase.java:1091)
        at liquibase.database.AbstractDatabase.executeStatements(AbstractDatabase.java:1075)
        at liquibase.changelog.ChangeSet.execute(ChangeSet.java:317)
        ... 7 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`openmrs`.`#sql-8a5_32`, CONSTRAINT `level_to_level` FOREIGN KEY (`level_id`) REFERENCES `address_hierarchy_level` (`address_hierarchy_level_id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
        at com.mysql.jdbc.Util.getInstance(Util.java:386)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1041)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617)
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778)
        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2819)
        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2768)
        at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:949)
        at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:795)
        at liquibase.executor.jvm.JdbcExecutor$1ExecuteStatementCallback.doInStatement(JdbcExecutor.java:92)
        at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:55)
        ... 11 more
25-01-2021 11:52:11 [WARN ] ModuleFactory - Error while trying to start module: emrapi
org.openmrs.module.ModuleException: Module EMR API Module cannot be started because it requires the following module(s): event 2.8.0 Please install and start these modules first.
        at org.openmrs.module.ModuleFactory.startModuleInternal(ModuleFactory.java:685)
        at org.openmrs.api.context.Daemon$1.run(Daemon.java:74)
25-01-2021 11:52:11 [WARN ] ModuleFactory - Error while trying to start module: reference-data
org.openmrs.module.ModuleException: Module Reference Data cannot be started because it requires the following module(s): addresshierarchy, emrapi Please install and start these modules first.
        at org.openmrs.module.ModuleFactory.startModuleInternal(ModuleFactory.java:685)
        at org.openmrs.api.context.Daemon$1.run(Daemon.java:74)
25-01-2021 11:52:11 [WARN ] ModuleFactory - Error while trying to start module: emrapi
org.openmrs.module.ModuleException: Module EMR API Module cannot be started because it requires the following module(s): event 2.8.0 Please install and start these modules first.
        at org.openmrs.module.ModuleFactory.startModuleInternal(ModuleFactory.java:685)
        at org.openmrs.api.context.Daemon$1.run(Daemon.java:74)
25-01-2021 11:52:11 [WARN ] ModuleFactory - Error while trying to start module: emrapi
org.openmrs.module.ModuleException: Module EMR API Module cannot be started because it requires the following module(s): event 2.8.0 Please install and start these modules first.
        at org.openmrs.module.ModuleFactory.startModuleInternal(ModuleFactory.java:685)
        at org.openmrs.api.context.Daemon$1.run(Daemon.java:74)
25-01-2021 11:52:11 [WARN ] ModuleFactory - Error while trying to start module: bacteriology
org.openmrs.module.ModuleException: Module Bacteriology Module cannot be started because it requires the following module(s): emrapi 1.17 Please install and start these modules first.
        at org.openmrs.module.ModuleFactory.startModuleInternal(ModuleFactory.java:685)
        at org.openmrs.api.context.Daemon$1.run(Daemon.java:74)
25-01-2021 11:52:11 [WARN ] ModuleFactory - Error while trying to start module: bahmnicore
org.openmrs.module.ModuleException: Module BahmniEMR Core OMOD cannot be started because it requires the following module(s): addresshierarchy, org.bahmni.module.reference-data, emrapi, bacteriology Please install and start these modules first.
        at org.openmrs.module.ModuleFactory.startModuleInternal(ModuleFactory.java:685)
        at org.openmrs.api.context.Daemon$1.run(Daemon.java:74)
25-01-2021 11:52:11 [WARN ] ModuleFactory - Error while trying to start module: elisatomfeedclient
org.openmrs.module.ModuleException: Module Open-Elis Atom Feed Client cannot be started because it requires the following module(s): org.bahmni.module.bahmnicore Please install and start these modules first.
        at org.openmrs.module.ModuleFactory.startModuleInternal(ModuleFactory.java:685)
        at org.openmrs.api.context.Daemon$1.run(Daemon.java:74)

My guess is that your address_hierarchy_entry table has invalid references to parent hierarchy.

I can’t rule that possibility out. A while back, when I was setting up the application, I ran into challenges with the address hierarchy module. I ended up manually deleting some DB entries and adding some. I don’t know if that could be the issue.

Here’s an old post of what I did with the Address Hierarchy back then

If possible for you, can you retry with a fresh OpenMRS database, or restore a previously working database?

It is likely that your address hierarchy tables are in invalid state due to manual edits.

Most likely it will be that the “parent_id” is not valid. You can write a query that checks whether the parent_id of a row is valid or not. For example, with the below query (do test it out, not validated)

SELECT

A1.address_hierarchy_entry_id, A1.name, A1.parent_id, A2. address_hierarchy_entry_id as parent_hierarchy_id

FROM address_hierarchy_entry AS A1 Left outer join address_hierarchy_entry A2 on A1.parent_id=A2.address_hierarchy_entry_id

if the output “parent_hierarchy_id” id is null or empty, then you know which entry the issue is.

Thanks @angshuonline

I see a couple of entries with NULL

cool. hope that helped. btw, parent_id can be null for level 1 entries :slight_smile: