[odb-users] polymorphic base orphans

Сергей Никонов shprotello at mail.ru
Sat Sep 12 01:38:20 EDT 2020


Hi everybody,
 
I encountered a problem concerned with appearing «orphan» records in the database representing base entities of a polymorphic hierarchy. An example below demonstrates the issue. There are a polymorphic abstract base class with two derived classes one of which contains a pointer to another.
 
#pragma db object abstract polymorphic
struct base
{
  virtual ~base() {}
#pragma db id auto
  int id;
};
#pragma db object
struct derived1
  : public base
{
#pragma db null
  int value;
};
#pragma db object
struct derived2
  : public base
{
#pragma db null
  int value;
#pragma db null on_delete(cascade)
  std::shared_ptr<derived1> ref;
};
 
After persisting objects like this:
 
    auto d1 = std::make_shared<derived1>();
    db.persist(d1);
    auto d2 = std::make_shared<derived2>();
    d2->ref = d1;
    db.persist(d2);
 
lets delete «d1» instance:
 
    db.erase<base>(d1->id);
 
After committing the database will contain an orphan record:
   id    typeid
   ----------------
   2    derived2
 
When I try to resolve the issue by manually deleting «base» instance in a database trigger I receive an exception from ODB.
Is there any reasonable way how to deal with this (quite typical in my opinion) situation in general?
 
Thank you in advance,
Sergey
 
 


More information about the odb-users mailing list