[odb-users] NULL Fields, how do they work ?

Boris Kolpackov boris at codesynthesis.com
Fri Jul 22 09:23:31 EDT 2011

Hi Nicolas,

Nicolas ALBEZA <n.albeza at gmail.com> writes:

> I'm trying to make a field nullable, but i can't find how in the manual.

For this you will need two things: 

1. A C++ type that is capable of representing the special NULL value.
   This can be, for example, a pointer as in:

   #pragma db object
   class object

     // NULL pointer is equivalent to the NULL value in the database.
     shared_ptr<std::string> str; 

   Or you can create a wrapper along these lines:

   struct nullable_string
     bool null;
     std::string str;

   #pragma db object
   class object
     nullable_string str;

2. Once you have a type that has the notion of NULL, you will need
   to provide a value_traits implementation for it. For an example,
   take a look at the tests/mysql/types test in the odb-tests
   package. There is a type called string_ptr which is used to
   test NULL-ness. The value_traits implementation is in the
   traits.hxx file.

Also, some of the types supported by profiles provide the notion
of NULL values and these type automatically store such values as
NULL in the database. Once such type QString in the Qt profile.

Finally, in one of the upcoming versions we are planning to add
support for the boost::optional container which has a natural
mapping to a column with NULL enabled. This will make working
with NULL values a much more straightforward matter if you are
using Boost. Perhaps we will also provide our own version of
something like this in libodb, in case you cannot use Boost
for some reason.


More information about the odb-users mailing list