PDA

View Full Version : QSqlRelationalTableModel and SubmitAll()



kroenecker
21st January 2008, 17:05
I have a relational table that uses a delegate to provide a drop down. When I change a drop down value and call submitall I get a SQL Server error that says I have an invalid column name.

The view displays all of the data properly so how can I have an invalid column name? Perhaps I need to manually perform the changes to the tables?

wysota
21st January 2008, 18:43
Which column name is invalid? How does the primary key for the table look like?

kroenecker
22nd January 2008, 02:37
The two columns that are being mapped to other tables appear to be the problem.

So in my case:

.... column n, column n + 1
state_id, user_id

When I set up the relation, I indicate which column from the state table and which column from the user table I want to replace the id values. In this case it's "state_name" and "user_name". Unfortunately SubmitAll() returns false and the error indicates that "state_name" and "user_name" are not valid column names.

Seems a bit weird to me that everything should display just fine but the submit causes problems.

wysota
22nd January 2008, 06:53
The relational table model can't insert rows to tables other than the "main" table of the relation. Maybe this is the problem in your case? If so, you have to first populate the "related" table and only then change the main table.

kroenecker
22nd January 2008, 14:34
I'm not sure if that is the problem. I'm not trying to insert into the related tables, I'm trying to submitall() when I change a value in an existing row. Shouldn't that work?

If it doesn't work that easily, then is there a way to query the model for rows that have had their data changed so that I can manually submit the changes to the related tables?

I appreciate your time Wysota.

wysota
22nd January 2008, 14:58
What about my question about the primary key of the table?

kroenecker
22nd January 2008, 15:29
Maybe that's the problem, the tables have int non-null columns named "id" but they aren't actually set as primary keys in the tables themselves. Do you think that is the problem?

Oh and each table has a column named "id".

I've got a related question: Is there a way for me to be able to view the sql statement that was internally built? If I could see that, it might help me understand this a bit better.

wysota
22nd January 2008, 15:53
Maybe that's the problem, the tables have int non-null columns named "id" but they aren't actually set as primary keys in the tables themselves. Do you think that is the problem?
That's the first thing I would check. The problem you are facing is surely related to the database and not to Qt itself.


I've got a related question: Is there a way for me to be able to view the sql statement that was internally built? If I could see that, it might help me understand this a bit better.
Yes, of course. The relational model inherits table model which in turn inherits QSqlQueryModel. So for peeking the select statement you have two choices - either through QSqlQueryModel::query() or QSqlTableModel::selectStatement(). QSqlTableModel::setPrimaryKey(), QSqlTableModel::primaryKey() are also worth having a look at. If you want to inspect the update query, you'll probably have to peek the query log of your SQL server or use QSqlQuery::lastQuery().