<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[[SOLVED] QSqlRelationalTableModel::insertRows + sqlite and autoincrementing primary keys]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">I'm using a sqlite database with the Qt sql module.</p>
<p dir="auto">My QSqlRelationalTableModel is viewed in a QTableView:</p>
<p dir="auto">@   ui-&gt;tableView-&gt;setModel(tableModel);@</p>
<p dir="auto">My table has a primary key with auto increment. In my user interface, I have a button for adding rows to the table.</p>
<p dir="auto">I add a row like this:</p>
<p dir="auto">@m_tableModel-&gt;insertRows(m_tableModel-&gt;rowCount(), 1);@</p>
<p dir="auto">However, when the new row appears in the view, the primary key is not filled in. I can enter data into the other fields, and <em>sometimes</em> that makes the primary id appear in the primary key field, but not always.</p>
<p dir="auto">I'm obviously doing something wrong. I'd appreciate any help, and "best practices" - type information.</p>
<p dir="auto">Thanks in advance!</p>
]]></description><link>https://forum.qt.io/topic/33381/solved-qsqlrelationaltablemodel-insertrows-sqlite-and-autoincrementing-primary-keys</link><generator>RSS for Node</generator><lastBuildDate>Wed, 12 Aug 2026 23:20:26 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/33381.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 22 Oct 2013 16:05:56 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [SOLVED] QSqlRelationalTableModel::insertRows + sqlite and autoincrementing primary keys on Tue, 22 Oct 2013 20:48:58 GMT]]></title><description><![CDATA[<p dir="auto">Found the answer here: ":<a href="http://qt-project.org/forums/viewthread/17746" target="_blank" rel="noopener noreferrer nofollow ugc">http://qt-project.org/forums/viewthread/17746</a>":<a href="http://qt-project.org/forums/viewthread/17746" target="_blank" rel="noopener noreferrer nofollow ugc">http://qt-project.org/forums/viewthread/17746</a></p>
<p dir="auto">I changed</p>
<p dir="auto">@m_tableModel-&gt;insertRows(m_tableModel-&gt;rowCount(), 1);@</p>
<p dir="auto">to</p>
<p dir="auto">@QSqlRecord newRecord = m_tableModel-&gt;record();<br />
newRecord.remove(0);<br />
m_tableModel-&gt;insertRecord(0, newRecord)@</p>
<p dir="auto">and it works.</p>
]]></description><link>https://forum.qt.io/post/200078</link><guid isPermaLink="true">https://forum.qt.io/post/200078</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Tue, 22 Oct 2013 20:48:58 GMT</pubDate></item></channel></rss>