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);
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_OBJECTQ_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_OBJECTQ_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 trueqDebug() << 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 ifthe 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.
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();
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)")
void medico::setEsp(especialidad *value)
{
esp = value;
setForeignKey("esp", value);
}
and the insertion works perfectly, thanks so much for the help :)