[odb-users] Postgresql column with default value

Mikhail Tomilov Mikhail.Tomilov at infotecs.ru
Tue Dec 3 08:32:48 EST 2013


Hello!

We have postgresql 9.1 table:

CREATE TABLE test (
     id bigserial primary key,
     some_field text not null default some_heavy_computation_procedure()
);

C++ code:
#pragma db object
struct test
{
     #pragma id auto
     long long id;

     std::string some_field;
};

test t;
db.persist( t );
std::cout << t.some_field; // expecting to get actual DB value 
calculated by some_heavy_computation_procedure()

With code above we get this SQL:
INSERT INTO test(id, some_field) VALUES(DEFAULT, $1) RETURNING id;

But we need to get SQL query like this:
INSERT INTO test(id, some_field) VALUES(DEFAULT, DEFAULT) RETURNING id, 
some_field;

We want to persist this class instance with DB default value for 
some_field. some_heavy_computation_procedure() is stored procedure that 
performs complex calculation we cannot implement in C++ code (for some 
reason).

We considered to use callbacks but didn't get it worked the way we want 
(described above). Also we tried to use struct like this:

#pragma db object table("test")
struct test_ins
{
     #pragma id auto
     long long id;

     // Intentionally absent some_field
};

This doesn't suit for polymorphic objects (because of typeid column and 
we had to perform additional select query after persist call).

What is the best approach to solve this problem?

Regards, Mikhail.



More information about the odb-users mailing list