PDA

View Full Version : QSqlRelationalTableModel Many to Many relation



lukeQt
30th March 2015, 18:48
Hi everyone,

The goal is to build a simple recipe ingredients app using pyqt.

I have three tables. The first table stores the recipes. The second table stores the ingredients. The third table stores the recipe ingredient join table.

QSqlRelationalTableModel setRelation states that the table's primary key may not contain a relation to another table. How do you handle a many to many relationship using pyqt then?

My original thought was I was going to have three QSqlRelationalTableModel to handle this.

Does this mean you cannot use QSqlRelationalDelegate and would have to create a custom delegate to handle the relationship? I did see this thread http://www.qtcentre.org/threads/46582-QSqlRelationalTableModel-editing-probllem.

Thank you,
Luke Kaim

wysota
31st March 2015, 09:04
How do you handle a many to many relationship using pyqt then?
Add a column that will be the primary key of the join table.

lukeQt
31st March 2015, 18:03
I have an additional question though. How does PyQt handle compound/composite keys though? If I only have one primary key, then there is nothing stopping the user from adding eggs twice to the same recipe. Does pyqt support this? If pyqt does not support this, which I suspect that it doesn't, then what workarounds do people use? Can you use a unique index to handle this? Thank you Wysota for responding.

Luke Kaim

wysota
31st March 2015, 18:48
If I only have one primary key
You always have one primary key. Remaining keys are secondary keys.


then there is nothing stopping the user from adding eggs twice to the same recipe.
No, because you can set a unique index on a pair of columns which are foreign keys from the tables you are linking.

Does pyqt support this?
PyQt has nothing to do with this. This is a feature of the database you are using. Qt is just a wrapper over the native client library for your dbms.

lukeQt
31st March 2015, 20:54
Thank you wysota. That is the way that I thought you could do it.

Luke