I have a class (ServicesTableModel
) which inherits from QSqlRelationalTableModel
. In the constructor I have:
ServicesTableModel::ServicesTableModel( QWidget* parent, QSqlDatabase db )
: QSqlRelationalTableModel( parent, db )
{
setTable( "servicios" );
select();
...
}
Now, if I place the line
qDebug() << primaryKey();
where the dots are I get
QSqlRecord( 1 )
" 0:" QSqlField("ser_id", int, required: no, generated: yes)
which makes perfect sense, but after
qDebug() << primaryKey().name();
the answer is
""
so I can't reach the name of the primary key.
The table is sqlite, defined with
CREATE TABLE servicios (ser_id integer primary key, ... )
This matters because I'm trying to generalize the class for objects closely related to the rows in a table, and it seems natural not to have to provide the primary key name to the constructor.
I must surely be doing something wrong, or I don't understand what the name()_
method from QSqlField
does.
primaryKey.name()
returns the name of the index.
In SQL, an index can be named, and this is independent from the name of the field(s) used for the key.
An index can work on several fields.
Each field name can be retrieved with key.fieldName(i)
, with 0<i<key.count()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With