<html><body><div style="color:#000; background-color:#fff; font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:10pt"><div><span>I'm just coming into this, but I don't see the problem. Just make DataMapper ignorant about the ID, but let the TableModel know about it.</span></div><div style="color: rgb(0, 0, 0); font-size: 13px; font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: transparent; font-style: normal;"><span>Also, SQLite has a implied RowID in addition (or instead of) your own autoincrement ID. Maybe that will help?</span></div><div style="color: rgb(0, 0, 0); font-size: 13px; font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: transparent; font-style: normal;"><span><br></span></div><div><br></div>  <div style="font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida
 Grande', sans-serif; font-size: 10pt;"> <div style="font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 12pt;"> <div dir="ltr"> <hr size="1">  <font size="2" face="Arial"> <b><span style="font-weight:bold;">From:</span></b> Knut Krause <knut.krause@lagom.de><br> <b><span style="font-weight: bold;">To:</span></b> interest@qt-project.org <br> <b><span style="font-weight: bold;">Sent:</span></b> Thursday, December 12, 2013 2:00 PM<br> <b><span style="font-weight: bold;">Subject:</span></b> Re: [Interest] Storing complex SQL relations with Qt<br> </font> </div> <div class="y_msg_container"><br>Thanks for your opinion. I thought I'd be at least this far right now but it <br clear="none">looks like I cannot insert new rows. I already opened a new thread<br clear="none"><br clear="none">[Interest] How to insert a new record with generated primary key into a <br clear="none">QSqlTableModel<br
 clear="none"><br clear="none">for that. But I've been readin all day long and I can't find a solution on how <br clear="none">I can initialize a new record with my DataMapper form …<br clear="none"><br clear="none"><br clear="none">Knut<br clear="none"><div class="yqt3855115747" id="yqtfd77474"><br clear="none">Am Donnerstag, 12. Dezember 2013, 21:24:09 schrieb Constantin Makshin:<br clear="none">> Usually if you don't explicitly ask for a transaction, RDBMS creates an<br clear="none">> implicit one for each statement. AFAIK, SQLite uses this idea too.<br clear="none">> <br clear="none">> And the next to last paragraph at <a shape="rect" href="http://www.sqlite.org/autoinc.html" target="_blank">http://www.sqlite.org/autoinc.html</a><br clear="none">> gives an impression that uniqueness of values in AUTOINCREMENT columns<br clear="none">> isn't affected by transactions, i.e. automatically generated customer<br clear="none">> IDs
 will be unique no matter what happens to your application after step 1.<br clear="none">> <br clear="none">> SQLite driver's lastInsertId() returns the result of<br clear="none">> sqlite3_last_insert_rowid(), so step 2 looks safe enough (according to<br clear="none">> SQLite documentation, INTEGER PRIMARY KEY [AUTOINCREMENT] column is just<br clear="none">> an alias to row ID). Just remember to get it from the same query object<br clear="none">> you use to insert the customer. ;)<br clear="none">> <br clear="none">> Conclusion — your idea looks good to me. Not very portable (different<br clear="none">> RDBMSes [may] have different notation of "last insert ID"), but should<br clear="none">> work with SQLite.<br clear="none">> <br clear="none">> On 12/12/2013 10:57 AM, Knut Krause wrote:<br clear="none">> > Hi,<br clear="none">> > <br clear="none">> > my application uses sqlite for its data and I
 modeled some complex<br clear="none">> > relations for customers, appointments and so on.<br clear="none">> > <br clear="none">> > Now I want to edit such a relation using a custom dialog where the<br clear="none">> > appointments go to a list and some other stuff to a table (all are<br clear="none">> > QSqlTableModels).<br clear="none">> > <br clear="none">> > The problem is: How do I insert the data depending on other things? Let's<br clear="none">> > say an appointment needs a customer. I insert the new customer data and<br clear="none">> > the appointments and when I click on submit I'd have to do something like<br clear="none">> > <br clear="none">> > 1. insert customer (I think with primary key autoincrement I can simply do<br clear="none">> > that)<br clear="none">> > <br clear="none">> > 2. retrieve the inserted customers ID. Can I use the models query() to
 get<br clear="none">> > lastInsertId()? Is this save?<br clear="none">> > <br clear="none">> > 3. Set this ID to all models that depends on customer (by hand?) and<br clear="none">> > submit<br clear="none">> > them as well<br clear="none">> > <br clear="none">> > Is this approach safe? I mean AFAIK there are no transactions involved and<br clear="none">> > what happens for example if I insert the customer and then my application<br clear="none">> > crashes leaving my database in an inconsistent state?<br clear="none">> > <br clear="none">> > Would be nice if someone could explain how I'm supposed to do this.<br clear="none">> > <br clear="none">> > regards<br clear="none">> > <br clear="none">> > <br clear="none">> > Knut<br clear="none">_______________________________________________<br clear="none">Interest mailing list<br clear="none"><a shape="rect"
 ymailto="mailto:Interest@qt-project.org" href="mailto:Interest@qt-project.org">Interest@qt-project.org</a><br clear="none"><a shape="rect" href="http://lists.qt-project.org/mailman/listinfo/interest" target="_blank">http://lists.qt-project.org/mailman/listinfo/interest</a><br clear="none"></div><br><br></div> </div> </div>  </div></body></html>