[odb-users] querying object and loading into a stack variable, then setting its id to 0 and persisting

MM finjulhich at gmail.com
Mon Jul 22 02:32:37 EDT 2024


On Mon, 22 Jul 2024 at 05:52, Boris Kolpackov <boris at codesynthesis.com>
wrote:

> MM <finjulhich at gmail.com> writes:
>
> > struct C {
> >   std::uint64_t id;   #pragma member id auto
> >  ....
> > };
> >
> > C myc;
> > db.query_one<future>(..., myc);
> > /// successful
> > myc.setid(0);
> > myc.changeotherattributes();
> >
> > db.persist(myc);
> >
> > /// this seems to INSERT a new object with value 0 for id, but my
> intention
> > is to clone the loaded object myc, change attributes, and let the
> database
> > determine the new id.
>
> Provided the C::id member is auto-assigned, this should do what you expect
> so I suspect there is something wrong with your setup. If you think this is
> a bug in ODB, please provide a complete reproducer and also specify which
> database we are talking about. In fact, ALWAYS specify which database we
> are talking about.
>
>
> > does odb keep track of the object memory addresses for determining
> whether
> > to UPDATE vs INSERT?
>
> No, ODB doesn't do anything like this.



This is sqlite. The stderr tracer shows a INSERT statement like so:

INSERT INTO "C" ("id", "attr1", "attr2") VALUES (?, ?, ?,)

I am confused as to why "id" is in the INSERT statement, given that it is
auto

The changelog.xml has this section for table C

      <primary-key auto="true">
        <column name="id"/>
      </primary-key>

The CREATE TABLE in sqlite has
PRIMARY KEY("id" AUTOINCREMENT),

I don't think it's a odb bug. The full picture is

struct Base {
  std::uint64_t id;   #pragma  member(Base::id) id auto
virtual(std::uint64_t)
};

template <typename base, typename derived>
class cloneable : public base
{
protected:
  using base::base;

  virtual base* clone() const override
  {
    return new derived{static_cast<derived const &>(*this)};
  }
};

struct C : public cloneable<Base,C> {
...
};


/// order of inclusion matters otherwise odb compiler crashes
struct C;
using derived_C = cloneable<Base,C>;
#pragma db object(derived_C) abstract definition


Does the fact that the id come from the base struct cause a problem, is it
because it's virtual in Base it doesn't auto increment?

Thanks


More information about the odb-users mailing list