<?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[How to get a record to insertRecord(-1, rec) in QSqlRelationalTableModel]]></title><description><![CDATA[<p dir="auto">What is the best way to get a record to insert a new row to the base table with foreign keys?</p>
<p dir="auto">When using record() or QSqlTableModel::record() after setRelation(...), the returned record contains fields with the column names of the display column.<br />
A inserRecord( -1, row ) with these fields fails, because the fields are not present in the table?</p>
<p dir="auto">I would expect that QSqlTableModel::record() returns a useful record, or that a record with display columns is transformed back to foreign key columns?</p>
<p dir="auto">My solution is to store a copy of original record before setting the relations.<br />
Is there a more generic solution?</p>
<p dir="auto">@<br />
SqlAttachmentModel::SqlAttachmentModel( QSqlDatabase db, QObject *parent)<br />
: QSqlRelationalTableModel(parent, db)<br />
{<br />
setTable( "Attachment" );</p>
<pre><code>// store record to use as template for insertRecord()
mBaseTableRecord = record();

setRelation( eAnimalId, QSqlRelation( "animal", "uId", "earTag"));
setRelation( eUnitId, QSqlRelation( "rumiWatchUnit", "uId", "serialNumber"));

// get useless record with display columns fields
mRelationalRecord = QSqlTableModel::record();
</code></pre>
<p dir="auto">}<br />
@</p>
]]></description><link>https://forum.qt.io/topic/5407/how-to-get-a-record-to-insertrecord-1-rec-in-qsqlrelationaltablemodel</link><generator>RSS for Node</generator><lastBuildDate>Sat, 05 Sep 2026 05:11:22 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/5407.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 28 Apr 2011 13:49:53 GMT</pubDate><ttl>60</ttl></channel></rss>