Importing organisation units from scratch, using SQL?

I am setting up organization unit for a project. There is a path and translation fields in the database table. Where is the value coming from or what is the logic for inserting the two?

Also I downloaded the latest demo database and realize that some fields such as type, latitude and longitude are omitted. Is it deliberate.

Ideally you should use the api to create objects in dhis2 not directly injecting/updating stuff via sql
it bypasses a lot of access right, validations, and calculated fields like the path property, cache invalidations.

here the path is deduced from the id or the orgunit and the parent id (and it’s ancestors)

to learn the api, I would advise that you use the app via the maintenance pages and look in the browser network view for the appropriate payload/method/endpoint.

You might also be interested by DHIS2 App Hub
to use the api from your browser

// press crtl-r to run
const api = await dhis2.api();
try {
  const ou = await api.post("organisationUnits", {
    name: "demo",
    shortName: "hello",
    openingDate: "2022-08-02"
  });
  return ou;
} catch (e) {
  return e;
}

the output

{
  "httpStatus": "Created",
  "httpStatusCode": 201,
  "status": "OK",
  "response": {
    "responseType": "ObjectReport",
    "uid": "bQcIqJyxjuy",
    "klass": "org.hisp.dhis.organisationunit.OrganisationUnit",
    "errorReports": []
  }
}

for longitude/latitude. the fields has been renamed and change a bit the type.
it’s now in db it’s postgis geometry field and the api is the geometry field.
previous it was a varchar coordinates and coordinate field

Ahamadyya Mission Cl
plnHVbJR6p4
{ “type”: “Point”, “coordinates”: [ -12.9487, 9.0131 ] }

3 Likes

@Stephan_Mestach thanks for the tip. In my case I am seeding data into the database from scratch. Is there a way to bulk upload data via the api? The part I am seeding data for is the maintenance app data. I am working on a blank db.

1 Like

you can feed a json ( or via the metatada importer it will show you what to do through the api)
depending on the size of what you want to import (async will be better)

from the documentation point of view you can have a look at

1 Like

The documentation for bulk upload is not clear.

Hi @jetisco4u ,

Which version of DHIS are you using? Also, the org unit data which you want in the new project, is this already present in other DHIS instance? Or are you creating them from scratch ?

Thanks,
Rithvik

  1. Version 2.38
  2. Creating from scratch. When I upload the data from the db with sql, the orunitgroupset and orgunitgroup show well in the frontend but the organisationunit data didn’t show even though it’s in the database. It is more convenient for me to insert the data from sql bc I am a database administrator. But if I get a clear documentation for bulk uploading the data I can also do it.

@jetisco4u ,

when you say you are inserting the data from sql, how are you generating the sql?

Thanks,
Rithvik

I create it myself using INSERT statement with the our current organization setting. I am currently on orgunit part.

There are so many documentation flying around so it’s difficult to know what is what?

I tried to add organisationunit through the maintenance app but there is no option to add the hierarchy level.

Hi @jetisco4u ,

Can you open server-url/api/organisationUnits.json?order=lastUpdated:desc and check if the org units you have added are already visible?

Yes, there is no option for hierarchy level in maintenance app because the new org unit will be added as child of the selected org unit from the org tree.

Thanks,
Rithvik

@rithvik I work around the SQL INSERT statement and the hierarchy is now working. However, the changes I made in the database reflect on the server api link server-url/api/organisationUnits.json?order=lastUpdated:desc as well as the database. But this changes don’t reflect yet in the maintenance app and the event report app even after I refresh the page, logout and login back.

Hello, there is a simpler method (csv) of importing org units into the DHIS2 via the import/export application.

check out this Metadata - DHIS2 Documentation

Feel free to contact me raphaelkenyuri@gmail.com for any assistance.

@jetisco4u Please feel free to ask for clarification or more explaination here as well. It will help updating the docs when there’s something not clear. And, I agree with @Kenyuri then Import/Export app is supposed to help you import Org Units, from scratch.

Thanks!

@Gassim @Kenyuri @rithvik thanks for the tips. I was able to upload the orgunit data using sql INSERT command. I want to create the metadata and will like to try to use the import app. Is there a sample json or csv document that can be upload with all the basic data?

1 Like

Hello @jetisco4u check out the documentation on the sample json or csv Metadata - DHIS2 Documentation

1 Like