title: Inserting row in QTableView
body: <p>I have a PostgreSQL parent table called 'rooms' and a related table called 'configurations'.  I have a form which would allow the user to add edit and add new configurations to individual rooms in a QTableView.  In the configuration table, room is a foreign key to the rooms table.  The form looks like this:</p>
  
  <p><a href="https://i.stack.imgur.com/xDv3p.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xDv3p.png" alt="enter image description here"></a></p>
  
  <p>As far as editing the configurations, there seems to be no issue.  The problem arises when I add a new record in the configurations model.  If I insert a row and do not set the room cell, the user can edit any of the fields and it will save to the database.  Although, when finished editing, the row clears and ! shows up in the row header.  If I reload the form, the record edited correctly.</p>
  
  <p>What I want to do, however, is to set the room number for the user and keep the room number field in the configuration view hidden from the user.  When I add a row and set the room number cell's value, I cannot edit the row.  Any ideas?</p>
  
  <pre><code>import sys
  from PyQt5.QtCore import *
  from PyQt5.QtGui import *
  from PyQt5.QtWidgets import *
  from PyQt5.QtSql import *
  from rooms import *
  from config import *
  
  
  class Setup_rooms(QDialog):
      def __init__(self):
          QDialog.__init__(self)
          self.ui = Ui_rooms_setup()
          self.ui.setupUi(self)
  
          self.rooms_model = QSqlRelationalTableModel(self)
          self.rooms_model.setTable('rooms')
          self.rooms_model.setRelation(4, QSqlRelation('room_types1', 'room_type', 'room_type'))
          self.rooms_model.setSort(int(self.rooms_model.fieldIndex("room")), Qt.AscendingOrder)
          self.rooms_model.select()
          self.rooms_mapper = QDataWidgetMapper(self)
          self.rooms_mapper.setSubmitPolicy(QDataWidgetMapper.AutoSubmit)
          self.rooms_mapper.setModel(self.rooms_model)
          self.rooms_mapper.setItemDelegate(QSqlRelationalDelegate(self))
          self.rooms_mapper.addMapping(self.ui.room_num, self.rooms_model.fieldIndex("room"))
          self.rooms_mapper.addMapping(self.ui.room_description, self.rooms_model.fieldIndex("description"))
          self.rooms_mapper.addMapping(self.ui.deep_clean_date, self.rooms_model.fieldIndex("deepcleandate"))
          self.rooms_mapper.addMapping(self.ui.deep_clean_cycle, self.rooms_model.fieldIndex("deepcleancycle"))
          room_types_model = self.rooms_model.relationModel(4)
          self.ui.room_type.setModel(room_types_model)
          self.ui.room_type.setModelColumn(room_types_model.fieldIndex('room_type'))
          self.rooms_mapper.addMapping(self.ui.room_type, self.rooms_model.fieldIndex('room_type'))
          self.rooms_mapper.toFirst()
          self.ui.current_index.setText(str(self.rooms_mapper.currentIndex()))
  
          self.config_model = QSqlRelationalTableModel(self)
          self.config_model.setTable('room_configurations')
          self.config_model.setRelation(self.config_model.fieldIndex("configuration"), QSqlRelation('configurations', 'configuration', 'configuration'))
          self.room_model = self.config_model.relationModel(self.config_model.fieldIndex('room'))
          self.config_view = self.ui.configurations
          self.config_view.setModel(self.config_model)
          self.config_view.horizontalHeader().setStretchLastSection(True)
          self.config_model.select()
          self.config_model.setHeaderData(self.config_model.fieldIndex("sort_order"), Qt.Horizontal, "sort order")
          self.config_view.setItemDelegate(View_room_config(self))
          self.config_view.setItemDelegate(QSqlRelationalDelegate(self.config_view))
          self.config_view.resizeColumnsToContents()
          room = self.rooms_model.record(self.rooms_mapper.currentIndex()).value('room')
          self.config_model.setFilter("room = '{0}'".format(room))
  
          #self.config_view.setColumnHidden(0, True)
          #self.config_view.setColumnHidden(2, True)
  
          self.ui.add_config.clicked.connect(self.add_config)
  
  
      def add_config(self):
          row=int(self.config_model.rowCount()) 
          self.config_model.insertRow(row) 
          self.index = QModelIndex(self.config_model.index(row, 2)) 
          self.config_model.setData(self.index, self.ui.room_num.text(), Qt.EditRole)
  
  
  
  class View_room_config(QSqlRelationalDelegate):
      def __init__(self, parent=None):
          QItemDelegate.__init__(self)
  
      def createEditor(self, parent, option, index):
          if index.column() == 3:
              combo = super().createEditor(parent, option, index)
  
  
  if __name__=="__main__":
      app=QApplication(sys.argv)
      db = QSqlDatabase.addDatabase("QPSQL")
      db.setHostName(host)
      db.setDatabaseName(database)
      db.setUserName(user)
      db.setPassword(password)
      if (db.open()==False):
          QMessageBox.critical(None, "Database Error", db.lastError().text())
      myapp = Setup_rooms()
      myapp.show()
      sys.exit(app.exec_())
  </code></pre>