Unable to create Category with existing CategoryOptions

Hi everyone,

I’m using the /api/metadata endpoint to create Categories and Category Options.

I created the Category Options successfully, then tried to create a Category that references those existing Category Options.

The import failed with:

Invalid reference [null] (CategoryOption)

However, if I remove the categoryOptions association, the Category is created successfully, and I can then link the same Category Options manually through the UI.

I reproduced the same behavior on play.im.dhis2.org using both 2.42.5.1 and 2.43.1.

Is this expected behavior, or should this work?

Thank you.

Hi @rehabm

I have not tested creating the category+categoryOptions using the metadata endpoint and would like to do that if you’d provide the entire payload you are using please; additionally, my first suspicion is that you might need to include required import parameters → Metadata - DHIS2 Documentation .

Specifically, I see that you are using code for the categoryOptions which means you would need to use the identifier parameter to set the identifier scheme to use CODE.

To be on the safe side, it is possible to create categories and then add the category options using the /api/categories, but I think you’re trying the metadata endpoint at the same time?

If it’s not a requirement to create those at the same time then I’d suggest to test using the dedicated /api/categories & /api/categoryOptions endpoints. Metadata - DHIS2 Documentation

Hi, rehabm

Yes, this should work. A Category can be created through the /api/metadata endpoint with existing Category Options associated with it.

From the error and payload in your screenshot, the issue appears to be with how the Category Options are being referenced, rather than with the Category–CategoryOption relationship itself.

The metadata importer uses UID as the default identifier scheme. In your payload, the Category Option is being referenced using its code (LT12). Try referencing the existing Category Option using its UID instead.

Alternatively, if you want to reference metadata objects by their codes, you can explicitly set the metadata import identifier scheme to CODE in the request.

The Invalid reference [null] (CategoryOption) error suggests that DHIS2 is failing to resolve the Category Option reference during the import.

Since you have confirmed that the Category can be created first and the same Category Options can subsequently be associated through the UI, the Category–CategoryOption relationship itself is supported. This therefore looks more like an identifier resolution issue during metadata import than an unsupported operation.

I would first try using the UIDs of the existing Category Options when creating the Category. If that still fails, it would be useful to see the exact metadata payload used to create the Category Options and the Category, as well as the import parameters.

The DHIS2 Metadata API documentation provides more details on the supported identifier schemes.

Hi @Gassim and @e4eDHIS2,

Thank you both for your suggestions.

I have already tested using the UIDs of the existing Category Options instead of the codes, but the Category Options still could not be associated with the Category.
So, the same issue occurs with both CODE and UID references.

Thanks @rehabm for letting us know. Thanks @e4eDHIS2 for the helpful post..

Would you give the other endpoints a try and see whether it works or fails as well?

Thanks!

Thanks,@Gassim. I retested this using the dedicated endpoints as suggested.

I also re-tested the metadata import using the UIDs of existing CategoryOptions. This time, it worked successfully, which is different from my previous test where it resulted in an error.

Hi @rehabm

So glad it worked… May I ask what changed this time? :grinning_face:

Hi @Gassim,

To be honest, I’m not exactly sure what changed. :sweat_smile:
I just repeated the same steps, and this time it worked successfully.

Thanks @rehabm! There’s a chance there was a cache issue… :folded_hands: