Project

General

Profile

Querying Dbo::weak_ptr flushes dirty objects

Added by Saif Rehman over 6 years ago

Hi,

I ran into a problem while trying to get Dbo::ptr out of a Dbo::weak_ptr. What I did was similar to the following

Wt::Dbo::ptr<Entity> entityPtr = dboSession.find<Entity>(...);
assert(entityPtr);

Wt::Dbo::ptr<IncomeCycle> cyclePtr = dboSession.add(new IncomeCycle());
Wt::Dbo::ptr<Account> accountPtr = entityPtr->accountWPtr.query(); //error executing prepared statement: account_id cannot be null
cyclePtr.modify()->accountPtr = accountPtr;

The problem is that I'm trying to set the IncomeCycle::accountPtr(the 'account_id') of cyclePtr, however, the weak ptr query function flushes the dirty object cyclePtr before I can set the accountPtr, and since accountPtr has a NotNull constraint, cyclePtr cannot be flushed and throws an exception.

Although I can work around this by either querying the weak_ptr before dboSession.add(new IncomeCycle()) or by using dboSession.find instead, however, I felt that I should report my issue since I don't believe that the session should have been flushed to query a weak_ptr. I'm not sure if this is a bug or not, so please decide for yourself.


Replies (2)

RE: Querying Dbo::weak_ptr flushes dirty objects - Added by Saif Rehman over 6 years ago

The IncomeCycle::accountPtr has a NotNull constraint

RE: Querying Dbo::weak_ptr flushes dirty objects - Added by Koen Deforche over 6 years ago

Wt::Dbo will indeed always flush before executing a query to make sure that there is no inconsistency caused by modifications. In this case, you could indeed argue that there will not be an inconsistency, but such more specific logic has not been implemented. It would be nice though; can you file this as a feature request ?

    (1-2/2)