objectbox/objectbox-python

Relations support

vaind opened this issue · 3 comments

vaind commented

It would be useful to have support for relations: standalone many-to-many as well as property-based to-one relations, see e.g. Go relations. To implement the minimal, the following needs to be addressed:

  • add property & standalone relation definitions support to the model
  • for standalone relations: add box methods exposing c-api obx_box_rel_*
  • add necessary c-api callbacks to objectbox/c.py

@vaind I want to implement relations and have a few questions:

  • when I add a Property with PropertyType.Relation and execute the example I get following error:
    objectbox.c.CoreException: 10501 (SCHEMA) - Has no target entity ID: Property info (my property) (2, Relation)
    
    How can I add a target id to the property?
  • is there an example how the c objectbox library does this? I couldn't find the implementations on github

If you could share some code that might help to understand the context better. But, I'll try anyway... 🙂

This is an example from an internal test; it's a tree with branches and leaves. A leaf has a parent relation to the branch:

    obx_model_entity(model, "DataLeaf", 1, 6348606983323322409);
    if (syncEnabled) obx_model_entity_flags(model, OBXEntityFlags_SYNC_ENABLED);
    obx_model_property(model, "id", OBXPropertyType_Long, 1, 7352554494005967060);
    obx_model_property_flags(model, OBXPropertyFlags_ID);
    obx_model_property(model, "parentId", OBXPropertyType_Relation, 2, 6351202743278204154);
    obx_model_property_flags(model,
                             (OBXPropertyFlags) (OBXPropertyFlags_INDEXED | OBXPropertyFlags_INDEX_PARTIAL_SKIP_ZERO));
    obx_model_property_relation(model, "DataBranch", 1, 7077739940711775368);

As you can see you need some flags to enable the index for the relation property.

Also, obx_model_property_relation is likely of interest, as it tells to which entity type the relation refers to. Here are the C API docs for it:

/// Refine the definition of the property declared by the most recent obx_model_property() call, declaring it a
/// relation.
/// @param target_entity The name of the entity linked to by the relation
/// @param index_id Must be unique within this version of the model
/// @param index_uid Used to identify relations between versions of the model. Must be globally unique.
OBX_C_API obx_err obx_model_property_relation(OBX_model* model, const char* target_entity, obx_schema_id index_id,
                                              obx_uid index_uid);

Hope that helps in a way. Note that we are a bit busy at the moment, I hope that we can start looking into your PRs in about 2 weeks.

I've created a PR to discuss this further