[odb-users] Postgresql column with default value
boris at codesynthesis.com
Wed Dec 4 03:02:29 EST 2013
Mikhail Tomilov <Mikhail.Tomilov at infotecs.ru> writes:
> We considered to use callbacks but didn't get it worked the way we
> want (described above).
Unfortunately there is still no support for the RETURNING clause
in ODB. It is on our TODO list but is not yet implemented.
For now, the workaround is the same as what I have described in
my previous reply:
That is, send the NULL value on persist() and load the computed
data member in a callback. Unfortunately, this will require a
separate query, but that's the best that can be done until proper
support for RETURNING is implemented.
Here is an outline:
// file: test.hxx
#pragma db object callback(init)
#pragma id auto
long long id;
init (odb::callback_event, odb::database&) const;
#pragma db view object(test)
// file: test.cxx
init (odb::callback_event e, odb::database& db) const
// Reset some_field to NULL so that the default value is
// assigned by the database.
const_cast<test*> (this)->some_field.reset ();
// Load the computed fields using the test_computed view.
odb::query<test_computed>::id == id).begin ().load (v);
const_cast<test*> (this)->some_field = v.some_field;
You can further optimize this by preparing and caching the query
used to load computed members (Section 4.5, "Prepared Queries").
More information about the odb-users