[odb-users] Why do I get an odb::object_not_persistent error in this test program?

Daniel daniel.the.programmer at gmail.com
Wed Oct 30 07:27:52 EDT 2013


Hi Boris,

Thank you for your quick reply.

I've added
database_global->update(parent.get());
after
parent->children.erase(parent->children.begin());

but the same error still occurs.

I've also upgraded to odb-2.3.0 and "--fkeys-deferrable-mode immediate" 
to the compiler line.
It now looks like this:
odb -p boost/date-time -d mysql --std c++11 --generate-query 
--fkeys-deferrable-mode immediate --generate-schema models.h

What else could be wrong?

Thanks,

Daniel


On 10/30/2013 06:11 AM, Boris Kolpackov wrote:
> Hi Daniel,
>
> Daniel <daniel.the.programmer at gmail.com> writes:
>
>> I'm getting an odb::object_not_persistent error.
> In your updater_thread() function you have this code:
>
> if(parent->children.size() > 0)
> {
>    database_global->erase<Child>((*parent->children.begin())->id);
>    parent->children.erase(parent->children.begin());
> }
>
> That is, you erase the child object from the database and then
> erase it from the parent's list of children. But you do not
> update the parent to reflect this change in the database. In
> other words, you need to add something like this:
>
> database_global->update (*parent);
>
> Note also that normally you should have gotten a foreign key
> constraint violation at the end of this transaction. However,
> because MySQL does not support deferred foreign keys, ODB
> generates all such constraints commented out (you can see
> that in the generated .sql file). As a result, you get the
> object_not_persistent exception in some other transaction
> instead, which makes it quite a bit harder to figure out.
>
> Also, in the upcoming release we've added an option to the
> ODB compiler to use immediate constraints. While enabling
> this will require a lot more discipline in the order in
> which you persist/erase related objects, it will also make
> it easier to debug cases like this.
>
> Boris



More information about the odb-users mailing list