Repository navigation
Foreign key combobox in table browser #614 - #1550
Conversation
This adds a combobox for the editor delegate when the associated column has a foreign key constraint. In this way the user can choose the key value based from a combo-box. Known problems: - When the editor delegate is activated the combobox switches to the first value. It should preserve the current value, as intended in setEditorData. -The encoding of the foreign table is the one from the current table, but they might be different (we allow that, although it's unconventional).
|
Apart from the known problems, I'm unsure about the interface change in sqlitetablemodel.h, but the alternative way to get the references was very cumbersome. The first mentioned knwon-problem is weird. I'm following this example and it doesn't work. I've tried also calling the parent implementation or different ways to find the data in the combo without success. |
The concurrent run of the query made the setEditorData function to not find the current value in the combo-box, because it was not actually any data loaded. Using completeCache() the data is loaded before trying to set the data in the editor. To minimise possible performance problems, the model only loads the foreign key field, instead of the whole related table.
# Conflicts: # src/sqlitetablemodel.h
|
I'm made a commit that solves the first problem that I had with the combo-box not getting the current value in the cell. I've also merged and solved the conflict with the master branch. I think this is in shape to be squashed and merged into the master branch, unless the second problem is considered a blocker. @MKleusberg do you want to review it or shall I merge it as is? |
| SqliteTableModel* fkModel = new SqliteTableModel(m->db(), parent, m->chunkSize(), m->encoding()); | ||
| sqlb::ObjectIdentifier tableId = sqlb::ObjectIdentifier(m->currentTableName().schema(), fk.table()); | ||
|
|
||
| QString column = fk.columns().at(0); |
There was a problem hiding this comment.
This line crashes when there are no columns. Like in this database:
CREATE TABLE a(x INTEGER PRIMARY KEY, y INTEGER);
CREATE TABLE b(p INTEGER, q INTEGER, FOREIGN KEY(q) REFERENCES a);|
I noticed a crash during my tests (just added a comment about that) which I think should be fixed before merging. But other than that I see no reason to not merge this 😄 |
|
One more thing: When the column allows NULL values we might want to add them as an option to the combobox. If not you are forced to select a value as soon as you edit the field. You can always set it to NULL later by using a keyboard shortcut or the context menu, so it's not super important to have it in the combobox but I think with the NULL option in there it would be easier to use and understand 😄 |
Except for not-null columns, a NULL value is given as possible value in the foreign-key combo-box. Avoid crash for tables referencing other without specifying the column.
|
@MKleusberg Thank you for the review. I've reproduced the crash and fixed it. Regarding the foreign keys that can be NULL, I've also added that value to the list as you suggests. It totally makes sense and it wasn't difficult to implement. It's already in the branch, in case you want to take a final look. |
|
Hello @mgrojo THANKS a lot for this new great improvement! Just out of curiosity... For instance, I have a drop-down list with 3 items to pick:
|
|
@SilvioGrosso The values are indeed obtained from the referenced table each time the drop-down list is used. For example, reusing the tables defined by Martin: CREATE TABLE a(x INTEGER PRIMARY KEY, y INTEGER);
CREATE TABLE b(p INTEGER, q INTEGER, FOREIGN KEY(q) REFERENCES a);Every value inserted in primary key If you delete, insert or update values in The NULL value is also added because it doesn't have a not-null constraint. But if you define b in this way: CREATE TABLE b(p INTEGER NOT NULL, q INTEGER, FOREIGN KEY(q) REFERENCES a);Then the dropdown won't have NULL included. If you want to try this tomorrow, I think there won't be any problem in merging this now. |
|
It will be a HUGE pleasure to test it! 👍 |
|
@SilvioGrosso Great! |
|
I don't see any more problems 😄 Feel free to merge this any time 😉 |
|
Hello @mgrojo Today, I have tested on Window 7 (32 bit) and Windows 10 (64 bit) this new very COOL feature! Here are my SQL steps to test this new option: CREATE TABLE SAMPLE ( CREATE TABLE DISEASE CREATE TABLE ANFI ( Everything works fine! Here is a screenshot on Windows 10 (64 bit) of the drop-down list: THANKS a lot indeed for all your efforts! |
|
@SilvioGrosso Thanks a lot for testing and reporting back. All the credit about "cascading" has to go to SQLite 😄 I suppose we can close now your issue #1523. |


This adds a combobox for the editor delegate when the associated column
has a foreign key constraint. In this way the user can choose the key value
based from a combo-box.
Known problems:
When the editor delegate is activated the combobox switches to the first
value. It should preserve the current value, as intended in setEditorData.
The encoding of the foreign table is the one from the current table, but
they might be different (we allow that, although it's unconventional).