[odb-users] Best Practice deleting persistent objects

Boris Kolpackov boris at codesynthesis.com
Fri Apr 4 06:14:56 EDT 2014


Hi Steven,

Steven Côté <steven.cote at gmail.com> writes:

>  #pragma db object
> class group
> {
>    ...
>    #pragma db value_not_null unordered
>    std::vector<lazy_shared_ptr<user> > Members_;
> };
> 
> #pragma db object
> class user
> {
>    ...
>    #pragma db value_not_null inverse(Members_)
>    std::vector<lazy_weak_ptr<group> > Groups_;
> };
> 
> Then I create a user and add it to a group. I then subsequently call
> erase() on that user. Now, I'm still getting the "FOREIGN KEY constraint
> failed" exception that I was getting before. I had thought that by adding
> the relationship to group from the user class, it would have auto-magically
> removed the corresponding entry from the group_Members table when erasing a
> user.

If you want to be able to erase user without having to clean any
references, then you need to put the non-inverse side of the
relationship in the user class.

Think about it this way: the non-inverse side is the only relationship
that actually exists in the database. So if erasing an object must also
take down the relationship, then the non-inverse side should be part of
that object.

Boris



More information about the odb-users mailing list