Important: Please read the Qt Code of Conduct -

SQL Record ID changes after submitAll

  • I'm trying to write some Python code to simply create a record in an SQL database (sqlite backend), and then get the newly created record. Code is called from within an QSqlTableModel.
    The odd thing is that when I do the following, I'm told the generated records id is 0. However after doing a submitAll, it changes. I'm guessing 1 is the starting value and correct, but why am I getting 0 when I use NULL to generate an ID?
    So I'm not sure how I can grab the record I've just inserted, as both the id and row of the record changes after submitAll.

    self.insertRows(0, 1)

        new_record = self.record(0) #Grab newly inserted record
        new_record.setNull("id")  #Generate an id
        # new_record.value("id") == 0
        #Get the record back out
        new_record = self.record(0)
        # new_record.value("id") != 0@

  • maybe you have auto_increment on id in your database ??

  • Just got "PRIMARY KEY" set on id

Log in to reply