QSqlTableModel::removeRow() does not delete row in SQLite database - c++

I use Windows, C++ and Qt 5.11.1. I have a simple single-table SQLite database. This table "templates" contains primary key: id INTEGER PRIMARY KEY.When I try to remove a single row in "templates" (using QSqlTableModel), m_model.removeRow(0) returns true and submitAll() returns true. But the table still owns that row. I've found that Qt generated the following SQL:
DELETE FROM templates WHERE "templates"."id" IS NULL.
I suppose that the problem is linked with QSqlRecord::isGenerated. How can I fix this error?

As QSqlTableModel::removeRows documentation :
Deletions are submitted immediately to the database. The model retains a blank row for successfully deleted row until refreshed with select().

Sorry, the reason was in overriden data() method in QSqlTableModel descendant. record() method required data(index, Qt::EditRole).

Related

hbm2ddl.auto update and new boolean property added to an existing table with records

I have a mapped entity with JPA in a PostgreSQL database.
The table exists and I have some records in it, now I want to add a simple new boolean (not Boolean) property.
In logs I can correctly see the alter table using not null because I chose boolean and not Boolean, it is all right but....
without seeing any errors, the database isn't being updated.
Trying to execute the alter table directly in my SQL client finally I can see the problem, that is the column that I'm just adding contains null values...
Obviously already existent records will have null values as soon as that column will be created.
That said, what could I do if I want to create a not null property in a table having already at least one record?
Thanks
From what i understood from your question, you are getting issues with your earlier added table rows when you update your table by adding a new boolean column.
Your previous table rows gets null values for the newly added column.
You can set the default values on your JPA entity properties using columnDefinition provided as an attribute in #Column annotation.
If you want to add a new boolean property in your JPA entity you can try this
#Column(name = "is_active", columnDefinition="tinyint(1) default 1")
private boolean isActive;
And since you are using hbm2ddl, this will not only create a fresh boolean column for you but also give it a default of true. It will also set true for all the previously added rows in that table.
Hope it helps!

C++: How to set GUID to NULL instead of GUID_NULL?

I have a database which uses GUIDs instead of, say, an ordinary counter for the ID fields. But I can't seem to put NULL (instead of GUID_NULL) into such fields in DB even though. Yes, the field in the database does take NULL.
Let's say there is a parent-child relationship between two tables. So there is a parent and a child GUID references from one table to another. But the "root" parent does not have any parent and there I would like to be able to put NULL into its ParentUID database field. If I put GUID_NULL there then I will need a corresponding default row in the referenced table which has a GUID-value of GUID_NULL so that the foreign key constraint won't break.
Also, using GUID_NULL with default-rows at referenced tables will give me a resultset back when doing a standard join operation...which is not desirable.
They way it's done in code when inserting values into database is using a CCommand which takes structure that contains the values of the row fields to be inserted. One of these is a GUID type variable.
So it creates an SQL statement string looking like
INSERT INTO [tablename] (field1, field2, field3,...) VALUES(?,?,?,...)
and then in a loop there is something like:
command.field1 = 1;
command.field2 = 2;
command.GUIDField = ?????? //I want to put NULL here instead of GUID_NULL
command.Open(...);
I hope it is understandable what I wish to do and what the conditions in code are.
Thankful for help!
UPDATE:
Ok, it was very hard to exaplin correctly, but this is exactly what I want to do http://support.microsoft.com/kb/260900
Just that when I follow that example, it makes no difference...still I get FK constraint violation on insert so I suspect it is trying to insert GUID_NULL instead of NULL. :(
The link I had in my Update-section does work, my bad: http://support.microsoft.com/kb/260900
It is the answer to my problems, perhaps it will help someone else as well! :3

Update Query Will Not Work Due To Key Violations

I am trying to update a specific field in the "Claims" table of my 2010 Access Database. I keep receiving an error message that says there are key violations. Here is the SQL:
UPDATE Claims SET Claims.LS_Name = "JPN"
WHERE (((Claims.Responsibility2)=0));
Is there any reason, based on the above code, that it is not working?
Thanks in advance!
My first guess would be that there is a unique key on Claims.LS_Name and your update hits more than one row.
OK, I ran into this issue as well in Microsoft Access and think I have ways to solve versions of this. The intermediate table may not be necessary. My experience is that autonumber is the issue so conversion to number seems to work, but you have to delete relationships for Access to allow this change.
remove all relationships to the destination table.
change from autonumber to number.
make new empty tablethatincrements starting with the next higher key https://superuser.com/questions/288087/how-do-i-set-the-first-value-of-autonumber-in-access
update/query to the tablethatincrements table instead of destination.
update/merge from tablethatincrementsto to the destination table, including new higher keys.
remake relationships
OR In certain situations. (for mine the new keys matched the old)
something like this (may not be exact steps)
remove all relationships to the destination table.
delete ID/primary key in destination
merge/update to destination
create new ID/Primary key in destination (so it can be autonumber to renumber keys)
remake relationships
Anyway my next research item is to see how to never use autoincrement and do the unique keys using other methods so relationships do not have to be removed to change from autoincrement to number.

How to update QSqlTableModel after database connection has been changed changed

I have class which is derived from QSqlRelationalTableModel.
I use SQLite database.
And I need to change database file.
I close old database file, call SetDatabaseName("path to new file") and open new database file.
Now I just call select() for the model, but it returns false.
And if I call setTable("table") and only after that select(), everything works...
But name of the table is the same...
I didn't find any method which allows to inform a model that database connection has been changed....
Do you know a better way to inform the model?
Ok. I have returned to this topic once again.
After db is changed I have to call setTable() with the same table name to reinit table model.
And I did not find a better way how to reinit table view, co I call
pTableView->setModel(NULL);
pTableView->setModel(model);
This generates a lot of unnecessary code calls, but in other case table view does not know about changes in table model (for example columns count).
I did not find a better way to reinitialize QSqlTableModel and QTableView. Some ideas?
// create your model(parent=0, QSqlDatabase::database());
// set your table model->setTable("tablename");
tableview->setModel(this->model)
bool ok = model->select() // model should populate itself and refresh tableView as well
That was my fault :(
With help of sources I found in which cases select() returns false.
And one fieldname in the table of my new database misses one letter:( So, QSqlQuery with select returned error.
setTable call updates fieldnames of the table, so select returns true.

Django - Insert Without Returning the Id of the Saved Object

Each time the save() method is called on a Django object, Django executes two queries one INSERT and one SELECT. In my case this is usefull except for some specific places where each query is expensive. Any ideas on how to sometimes state that no object needs to be returned - no SELECT needed.
Also I'm using django-mssql to connect to, this problem doesn't seem to exist on MySQL.
EDIT : A better explanation
h = Human()
h.name='John Foo'
print h.id # Returns None, No insert has been done therefore no id is available
h.save()
print h.id # Returns the ID, an insert has taken place and also a select statement to return the id
Sometimes I don't the need the retruning ID, just insert
40ins's answer was right, but probably it might have higher costs...
When django execustes a save(), it needed to be sure if the object is a new one or an existing one. So it hits the database to check if related objext exists. If yes, it executes an UPDATE, orherwise it executes an ISERT
Check documentatin from here...
You can use force_insert or force_update ,but that might be cause serious data integrity problems, like creating a duplicate entry instead of updating the existing one...
So, if you wish to use force , you must be sure whether it will be an INSERT or an UPDATE...
Try to use save() method with force_insert or force_update attributes. With this attributes django knows about record existence and don't make additional query.
The additional select is the django-mssql backend getting the identity value from the table to determine the ID that was just inserted. If this select is slow, then something is wrong with your SQL server/configuration because it is only doing SELECT CAST(IDENT_CURRENT(*table_name*) as bigint) call.