Nominate our 2022 Qt Champions!

Getting primary key after calling QSqlTableModel::submitAll()

  • I am working on a order form, there are two QSqlTableModel's: orderModel and orderItemModel. In both cases the models start off empty. The DB, SQLite, has a AUTOINCREMENT column for the PK. The submitAll() needs to be called on the orderModel first so the orderItemModel foreign key can be set before it is inserted.

    How do I get the primary key from this inserted row?

    In the beforeInsert() slot of the orderModel, the PK column is being set to not be generates so the SQLite’s AUTOINCREMENT can do it thing:

    record.setGenerated(_orderModel->orderIdNo(), false);*

    Side note: I know for an absolute FACT, in the very near feature the DB will be converted over to Firebird, which does NOT have a AUTOINCREMENT concept like most DB’s.
    So ideally if there is a way to get the PK that will work today for SQLite and tomorrow for Firebird, that would be ideal!


  • So I am wondering... Most of my questions on this form get answered pretty quick, if I ask them early in the day, they are normally answered by the end of the day. I am wondering if the reason this has gone unanswered is because it is a unique question or is it simply those that are around don't do a lot of database stuff? I would have figured this is a pretty common question:)

Log in to reply