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

MM finjulhich at gmail.com
Wed Jul 31 14:15:52 EDT 2024


>> [...] the tracer shows that the INSERT INTO query still uses the
>> 'id' column?
>>
>> From a first look as to how I wrote the Base::id  member pragma, do you
>> think the generated SQL stmt should not use column id because the
database
>> will take care of that?

>No, this is expected. In SQLite, to get an auto-generated id, you insert
>a NULL value into the corresponding column (and if you insert a non-NULL
>value, then SQLite uses your value, so you can have it both ways at the
>same time).

>I looked through the generated code for your example and I don't see
>anything wrong with it. You can see the id image is unconditionally
>set to NULL in the generated persist() function for Derived.

>Are you actually observing wrong values in the database or are you
>basing your assumption that something is wrong merely on the fact
>that id is present in INSERT?

I load 1 object with
Derived d;
db.query_one(..., d)
which returns true, and a Derived object is loaded.

Then I change d's attributes, in particular I set d.id to 0, and other
attributes, then call

db.persist(d);

This inserted a row with id 0, then the next time around "duplicate"
because id is already present

I mean this is really the crux of the question.  How do I load a row into
d, "copy" it to another row, with the id being automatic, how to set id to
NULL?

The question is both sqlite and portsgresl.   the id is of c++ type
std::uint64_t


More information about the odb-users mailing list