<?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 relate two tablemodels together properly?]]></title><description><![CDATA[<p dir="auto">Hey everyone,<br />
I have two tableviews, each with their respective QSqlTableModel (or QSqlRelationalTableModel). One for appointments, the other for patients. The sqlite tables are set up like this:</p>
<p dir="auto"><strong>patient_profile</strong></p>
<ul>
<li>patient_id (primary key)</li>
<li>first_name</li>
<li>last_name</li>
</ul>
<p dir="auto"><strong>appointments</strong></p>
<ul>
<li>appt_id (primary key)</li>
<li>patient_id (foreign key to patient_profile)</li>
<li>first_name</li>
<li>last_name</li>
<li>appointment_date_time</li>
</ul>
<p dir="auto">Is there a proper way to tie the patient_id, first and last names to the appointment tablemodel?<br />
Looking through the documentation I see I can set a relation to a field, but will a change to the patient_profile table in the database trigger the appointment tablemodel to update? Or should I just carefully tell the model to update (select) when I suspect a change has happened?</p>
<p dir="auto">I hope my question is clear, thanks for your time.</p>
]]></description><link>https://forum.qt.io/topic/139533/how-to-relate-two-tablemodels-together-properly</link><generator>RSS for Node</generator><lastBuildDate>Thu, 05 Nov 2026 06:26:08 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/139533.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 23 Sep 2022 23:43:24 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to relate two tablemodels together properly? on Thu, 29 Sep 2022 00:38:26 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> thanks so much for the answer Jon. I will take a look at those links. I figured the appointment table shouldn't carry redundant information.  Appreciate your time.</p>
<p dir="auto">EDIT: To anyone that finds this via search, the last link Jon provided (<a href="https://stackoverflow.com/questions/36437055/qsqlrelationaltablemodel-additional-related-columns/69987711#69987711" target="_blank" rel="noopener noreferrer nofollow ugc">https://stackoverflow.com/questions/36437055/qsqlrelationaltablemodel-additional-related-columns/69987711#69987711</a>) seems like the best approach</p>
<p dir="auto">EDIT2: Unfortunately, although though the link above provides a great way to view data, there is no easy way to edit (or insert into model). Recommend going with a one-to-one relation.</p>
]]></description><link>https://forum.qt.io/post/730037</link><guid isPermaLink="true">https://forum.qt.io/post/730037</guid><dc:creator><![CDATA[judethedude]]></dc:creator><pubDate>Thu, 29 Sep 2022 00:38:26 GMT</pubDate></item><item><title><![CDATA[Reply to How to relate two tablemodels together properly? on Sat, 24 Sep 2022 08:48:27 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/judethedude">@<bdi>judethedude</bdi></a> said in <a href="/post/730005">How to relate two tablemodels together properly?</a>:</p>
<blockquote>
<p dir="auto">Looking through the documentation I see I can set a relation to a field, but will a change to the patient_profile table in the database trigger the appointment tablemodel to update?</p>
</blockquote>
<p dir="auto">Absolutely <em>not</em>!  Neither in Qt models nor in the SQL database.</p>
<p dir="auto">What you are <em>supposed</em> to do with a foreign key is <em>not</em> store further information from foreign table columns also back in the referencing appointments table.  This is one of the "fundamentals" and relational databases.</p>
<p dir="auto">An <em>appointment</em> does not have a first or last name.  Only a <em>patient</em> has those.  If looking at an appointment and you want patient information you are supposed to use the foreign key to go get that.<br />
,<br />
For your second question, Google for <code>qsqlrelationaltablemodel multiple columns</code>, that is what you are trying to do here.  I don't think Qt/<code>QSqlRelation</code> handles this for you.  Have a look through the suggestions at e.g. <a href="https://stackoverflow.com/questions/36437055/qsqlrelationaltablemodel-additional-related-columns" target="_blank" rel="noopener noreferrer nofollow ugc">https://stackoverflow.com/questions/36437055/qsqlrelationaltablemodel-additional-related-columns</a>.  There are others (e.g. <a href="https://stackoverflow.com/questions/35536730/qsqlrelationaltablemodel-two-references-to-the-same-table-same-foreign-keys" target="_blank" rel="noopener noreferrer nofollow ugc">https://stackoverflow.com/questions/35536730/qsqlrelationaltablemodel-two-references-to-the-same-table-same-foreign-keys</a>), so far as I can see all in a similar vein.  Note that with these solutions you end up with your <code>appointments</code> table being not editable.  Or perhaps, per the approach in <a href="https://stackoverflow.com/a/69987711/489865" target="_blank" rel="noopener noreferrer nofollow ugc">https://stackoverflow.com/a/69987711/489865</a></p>
<blockquote>
<p dir="auto">There is only one problem if you want to edit values through a QTableView: You can edit other columns, but if you try to edit the related column, the model does not know what to do.</p>
</blockquote>
]]></description><link>https://forum.qt.io/post/730017</link><guid isPermaLink="true">https://forum.qt.io/post/730017</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sat, 24 Sep 2022 08:48:27 GMT</pubDate></item><item><title><![CDATA[Reply to How to relate two tablemodels together properly? on Sat, 24 Sep 2022 00:04:58 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/judethedude">@<bdi>judethedude</bdi></a></p>
<p dir="auto">A follow-up question, since I want first_name and last_name to be mapped as well as patient_id, but only patient_id is guaranteed to be unique...how would I set that up?</p>
<pre><code>appt_model.setRelation(1, QSqlRelation("patient_profile", "patient_id", "patient_id"))
</code></pre>
<p dir="auto">That would map patient_id from appointments to patient_profile, but how would I set up the first_name and last_name...would I need to restructure my appointment table to something like this?<br />
<strong>appointments</strong></p>
<ul>
<li>appt_id</li>
<li>patient_id</li>
<li>patient_id_first_name (value is the same as patient_id)</li>
<li>patient_id_last_name (value is the same as patient_id)</li>
</ul>
<pre><code>appt_model.setRelation(2, QSqlRelation("patient_profile", "patient_id", "first_name"))
</code></pre>
<p dir="auto">I'm a bit hazy on this subject so any advice would be appreciated.</p>
]]></description><link>https://forum.qt.io/post/730006</link><guid isPermaLink="true">https://forum.qt.io/post/730006</guid><dc:creator><![CDATA[judethedude]]></dc:creator><pubDate>Sat, 24 Sep 2022 00:04:58 GMT</pubDate></item></channel></rss>