I'm having a bit of trouble with QSqlRelationalTableModel. Every time I submitAll(), the value of a relational column (Customers.name) is written to my table instead of the integer key (Projects.customerId). Below is some pseudo code. Any suggestions for how to debug and/or resolve this issue?

The field correctly reads (customerNameLineEdit gets populated with the correct value) however a problem occurs on write. I've tried with and without foreign keys. I'm using MySql as my database.

Qt Code:
  1. CREATE TABLE IF NOT EXISTS `projects` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `customerId` int(11) NOT NULL,
  4. `industryId` int(11) DEFAULT NULL,
  5. `opCode` varchar(255) DEFAULT NULL,
  6. `statusId` int(11) DEFAULT NULL,
  7. `startDate` date DEFAULT NULL,
  8. `endDate` date DEFAULT NULL,
  9. `notes` text,
  10. PRIMARY KEY (`id`),
  11. UNIQUE KEY `opCode` (`opCode`),
  12. KEY `customerId` (`customerId`),
  13. KEY `status` (`statusId`),
  14. KEY `industryId` (`industryId`),
  15. KEY `customerId_2` (`customerId`)
  16. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=39 ;
  17.  
  18. CREATE TABLE IF NOT EXISTS `customers` (
  19. `id` int(11) NOT NULL AUTO_INCREMENT,
  20. `name` text,
  21. PRIMARY KEY (`id`)
  22. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=16 ;
  23.  
  24.  
  25. // SETUP THE TABLE
  26. projectsModel.setTable("Projects");
  27. projectsModel.setRelation(1, QSqlRelation("Customers", "id", "name"));
  28. projectsModel.setRelation(2, QSqlRelation("refIndustries", "naicCode", "name"));
  29. projectsModel.setRelation(4, QSqlRelation("refStatuses", "id", "name"));
  30. projectsModel.select();
  31.  
  32. mapper = new QDataWidgetMapper(this);
  33. mapper->setModel(projectModel);
  34. mapper->setItemDelegate(new QSqlRelationalDelegate(mapper));
  35. mapper->addMapping(ui->customerNameLineEdit, projectModel->fieldIndex("Customers_Name"));
  36. mapper->addMapping(ui->industryComboBox, projectModel->fieldIndex("refIndustries_name"));
  37. mapper->addMapping(ui->projectCodeLineEdit, projectModel->fieldIndex("opCode"));
  38. mapper->addMapping(ui->statusComboBox, projectModel->fieldIndex("refStatuses_name"));
  39. mapper->addMapping(ui->startDateDateEdit, projectModel->fieldIndex("startDate"));
  40. mapper->addMapping(ui->endDateDateEdit, projectModel->fieldIndex("endDate"));
  41. mapper->addMapping(ui->notesTextEdit, projectModel->fieldIndex("notes"));
  42. mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
  43. mapper->setCurrentIndex(rowId);
  44.  
  45.  
  46. // LATER I DO A SUBMIT LIKE THIS
  47. mapper->submit())
  48. projectModel->submitAll()
To copy to clipboard, switch view to plain text mode