Hi all.

First review the code that I'm using to create the tables.

Qt Code:
  1. void CreateTablesInSQLServer()
  2. {
  3. QSqlQuery createGroupsTbl("CREATE TABLE grupa_tbl("
  4. "Grupa_Id int PRIMARY KEY IDENTITY(1,1) NOT NULL,"
  5. "GrupaName varchar(30) NOT NULL UNIQUE);");
  6.  
  7.  
  8. QSqlQuery createContactsTbl("CREATE TABLE contacts_tbl("
  9. "contact_id int PRIMARY KEY IDENTITY(1,1) NOT NULL,"
  10. "FirstName varchar(30) NULL,"
  11. "LastName varchar(40) NULL,"
  12. "Mobile varchar(40) NULL,"
  13. "Phone varchar(40) NULL,"
  14. "GrupaId int NOT NULL,"
  15. "FOREIGN KEY (GrupaId) REFERENCES grupa_tbl);");
  16. }
To copy to clipboard, switch view to plain text mode 





After that I'm creating the model and populate the model with data.
Qt Code:
  1. model->setTable("contacts_tbl"); //2
  2. int i = model->fieldIndex("GrupaId"); //3
  3. model->setRelation(i, QSqlRelation("grupa_tbl", "Grupa_Id", "GrupaName")); //4
  4. QString TableName = model->tableName(); //5
  5. model->select(); //6
  6. int k = model->fieldIndex("GrupaId"); //7
  7. int y = model->fieldIndex("LastName"); //8
  8. model->setTable("contacts_tbl"); //9
  9. int h = model->fieldIndex("GrupaId"); //10
To copy to clipboard, switch view to plain text mode 



This is the problem:
In line3 i can retrieve the field index for "GrupaId".
After i make the relation in line4 and i populate the model in line6, i cannot retrieve the field index for "GrupaId"(which is FOREIGN KEY) in line8. But i can get the field index for "LastName" and all of the rest columns.

If i call setTable in line9 i can retrieve the field index for "GrupaId", but as a result the relation in line4 is broken and i have to recreate it.

My goal is to retrieve the field index for "GrupaId" without calling
Qt Code:
  1. model->setTable("contacts_tbl");
To copy to clipboard, switch view to plain text mode 



again and again.


Thanks in advance.