Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QSqlField name() method returns ""

Tags:

c++

sql

qt

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.

like image 815
Queequeg Avatar asked Apr 22 '12 10:04

Queequeg


1 Answers

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()

like image 103
galinette Avatar answered Oct 22 '22 03:10

galinette