Re: Help - object doesn't insert to DB

112 views
Skip to first unread message

Jeremy Lainé

unread,
Jan 15, 2014, 11:57:28 AM1/15/14
to Horacio Benitez, qdj...@googlegroups.com

Looks like a problem with your primary key: you decided to manually define a primary key but did not assign it for your second object.

If your primary key is just an integer id no need to define one, QDjango will give you an auto increment field called "id".

Also you might consider enabling SQL debugging to see what the error is.

QDjango::setDebugEnabled(true);

On 15 Jan 2014 17:08, Horacio Benitez <beni...@gmail.com> wrote:
I recently started to use QDjango, i really like it, it does have all i need to my develop university thesis and im using it, my first problem its this one, i have two objects, here is the code :


//Code to create database conn and register models: 
void connection(){

    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setDatabaseName("prueba");
    db.setHostName("localhost");
    db.setUserName("root");
    db.setPassword("root");
    if(!db.open()) qDebug() << db.lastError().text();

    QDjango::setDatabase(db);
    QDjango::registerModel<especialidad>();
    QDjango::registerModel<medico>();
    QDjango::createTables();
}


//class especialidad :

class especialidad : public QDjangoModel
{
    Q_OBJECT
    Q_PROPERTY(int idEspecialidad READ getIdEspecialidad WRITE setIdEspecialidad)
    Q_PROPERTY(QString descripcion READ getDescripcion WRITE setDescripcion)
    Q_CLASSINFO("idEspecialidad", "primary_key=true")

public:
    especialidad();

    int getIdEspecialidad() const;
    void setIdEspecialidad(int value);

    QString getDescripcion() const;
    void setDescripcion(const QString &value);

private:
    int idEspecialidad;
    QString descripcion;
};



//class medico :

class medico : public QDjangoModel
{
    Q_OBJECT
    Q_PROPERTY(int numRegistro READ getNumRegistro WRITE setNumRegistro)
    Q_PROPERTY(int numCedula READ getNumCedula WRITE setNumCedula)
    Q_PROPERTY(especialidad *esp READ getEsp WRITE setEsp)
    Q_CLASSINFO("numRegistro", "primary_key=true")

public:
    medico();

    int getNumRegistro() const;
    void setNumRegistro(int value);

    int getNumCedula() const;
    void setNumCedula(int value);

    especialidad *getEsp() const;
    void setEsp(especialidad *value);

private:
    int numRegistro, numCedula;
    especialidad *esp;
};


//Code to create and save objects in a main window :


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    especialidad *esp = new especialidad();
    esp->setIdEspecialidad(1);
    esp->setDescripcion("veterinario");
    esp->save();

    medico *med = new medico();
    med->setNumRegistro(123);
    med->setNumCedula(380);
    med->setEsp(esp);
    med->save();

}


I've registered all models and succefully created tables in DB and all the PK's and FK's, but when i execute the code :

qDebug() << esp->save();
//returns true
qDebug() << med->save(); 
//returns false

Im I doing something wrong? object med doesnt saves in database, i guess something's wrong with FK in class medico, but im not sure what to do to see where is the error in the code or query to save the object, i hope someone can help, sorry if 
the error its stupid.

--
You received this message because you are subscribed to the Google Groups "QDjango" group.
To unsubscribe from this group and stop receiving emails from it, send an email to qdjango+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Horacio Benitez

unread,
Jan 15, 2014, 1:09:42 PM1/15/14
to qdj...@googlegroups.com, Horacio Benitez
Got it, i tested it without a primary key on class "especialidad" and it created a primary key auto-increment by itself, but on my creation of the objects :

especialidad *esp = new especialidad();
    esp->setIdEspecialidad(1);
    esp->setDescripcion("veterinario");
    esp->save();

    medico *med = new medico();
    med->setNumRegistro(123);
    med->setNumCedula(380);
    med->setEsp(esp);
    med->save();

my pointer esp doesnt have any info about its id on the database, so the debug was throwing this : 


SQL query "SELECT 1 AS a FROM "medico" WHERE "numRegistro" = ?"

SQL $1 = 123

SQL query "UPDATE "medico" SET "esp_id" = ?, "numCedula" = ? WHERE "medico"."numRegistro" = ?"

SQL $1 =

SQL $2 = 380

SQL $3 = 123

SQL error QSqlError(-1, "QPSQL: Unable to create query", "ERROR: null value in column "esp_id" violates not-null constraint

(23502)") 


and there it is, no value for esp_id, i guess i should use QDjangoWhere or queryset to bring the object from the database to do this kind of insertion.
However, i tested it again with a manual primary_key, but i added a line (this was my mistake) :

void medico::setEsp(especialidad *value)
{
    esp = value;
    setForeignKey("esp", value);
}

and the insertion works perfectly, thanks so much for the help :)
Reply all
Reply to author
Forward
0 new messages