[odb-users] database generated ID combined with manual ID in SQLite

Vladimir Yelnikov vyelnikov at adfsolutions.com
Wed Oct 26 04:53:30 EDT 2016


We have code looks like this:
struct SomeClass
{
  #pragma id auto
  int id_;
}
I need to combine autogenerated IDs with manual IDs when persisting
objects, sometimes developer needs to set IDs manually and in some cases,
he doesn't care.

The first solution I tried is using nullable IDs and rely on SQLite
behavior when id is NULL - id will be generated. But it requires a lot of
changes in code
struct SomeClass
{
  #pragma id auto
  odb::nullable<int> id_;
}

Also I tried to use --sqlite-lax-auto-id ODB compiler parameter, ODB
doesn't throw exception about monotonically inc id but it ignores custom
IDs.
Is it possible to get need behavior using ODB pragmas?

  void access::object_traits_impl< SomeClass, id_sqlite >::

  persist (database& db, object_type& obj)

{

...

    if (init (im, obj, statement_insert)) //producess proper id and image_type

      im.version++;

...

    im.id_null = true; // will be nice to get something like
im.id_null = im.id == 0;

...

}

Best Wishes,
Vladimir


More information about the odb-users mailing list