Could not insert when the type of id is long?

Forum for posting problems using QxOrm library

Could not insert when the type of id is long?

Postby behtgod » Fri Jul 01, 2016 7:30 am

This is my class:
Code: Select all
#ifndef TESTDATABASE_H
#define TESTDATABASE_H

#include "QxOrm.h"

class testDatabase
{
public:
    testDatabase();
    virtual ~testDatabase(){}

    long id;
    QString value;

};
QX_REGISTER_HPP_EXPORT_DLL(testDatabase, qx::trait::no_base_class_defined, 1)

#endif // TESTDATABASE_H


Code: Select all
#include "testdatabase.h"

QX_REGISTER_CPP_EXPORT_DLL(testDatabase)

namespace qx {
    template <> void register_class(QxClass<testDatabase> & t)
    {
        t.setName("t_test");
        t.id(&testDatabase::id,"id");
        t.data(&testDatabase::value,"value");
    }
}

testDatabase::testDatabase():
    id(0)
{

}


my main:
Code: Select all
testdatabaseptr d1=testdatabaseptr(new testDatabase);
d1->id=0;
d1->value="aaaa"+QString::number(0);
QSqlError daoError = qx::dao::insert(d1);


my table:
Code: Select all
CREATE TABLE t_test (id INTEGER NOT NULL, value TEXT, PRIMARY KEY (id));


When I use mysql database,i got the error message:
[QxOrm] execute sql query failed : INSERT INTO t_test (value) VALUES (:value)
QMYSQL3: Unable to execute statement
Field 'id' doesn't have a default value


I found that only the field 'value' would be inserted according to the sql.
Why?
And,how should I do if i want insert both id and value ?
behtgod
 
Posts: 18
Joined: Mon Nov 16, 2015 8:21 am

Re: Could not insert when the type of id is long?

Postby qxorm » Fri Jul 01, 2016 7:56 am

Hello,

I found that only the field 'value' would be inserted according to the sql. Why? And,how should I do if i want insert both id and value ?

Your id is a numeric type : so by default, QxOrm library considers that this is an auto increment field.

To change this default behaviour, you can write something like this :
Code: Select all
namespace qx {
    template <> void register_class(QxClass<testDatabase> & t)
    {
        t.setName("t_test");

        IxDataMember * pData = t.id(&testDatabase::id,"id");
        pData->setAutoIncrement(false);

        t.data(&testDatabase::value,"value");
    }
}
qxorm
Site Admin
 
Posts: 481
Joined: Mon Apr 12, 2010 7:45 am

Re: Could not insert when the type of id is long?

Postby behtgod » Mon Jul 04, 2016 5:39 am

Thank you very much!
behtgod
 
Posts: 18
Joined: Mon Nov 16, 2015 8:21 am


Return to QxOrm - Help

Who is online

Users browsing this forum: No registered users and 5 guests

cron