Help - object doesn't insert to DB

26 views
Skip to first unread message

Horacio Benitez

unread,
Jan 15, 2014, 11:08:33 AM1/15/14
to qdj...@googlegroups.com
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.

Reply all
Reply to author
Forward
0 new messages