Bug report #13089

DBManager edit table dialog bug on notNull values

Added by Médéric RIBREUX over 9 years ago. Updated over 9 years ago.

Status:Closed
Priority:Normal
Assignee:-
Category:DB Manager
Affected QGIS version:2.10.0 Regression?:No
Operating System: Easy fix?:No
Pull Request or Patch supplied:Yes Resolution:
Crashes QGIS or corrupts data:No Copied to github as #:21156

Description

Hello,

I 've found a bug in the edit table dialog of DBManager.

Whenever you try to edit a field, the notNull attribute is never retrieved (the checkBox "can be Null" is always checked even if the field has a NOT NULL constraint).

I think that the problem comes from the TableFieldsModel class in db_plugins/data_model.py.
The getObject method which is called when you click on the "Edit column" button always returned a False value because of the following line:

fld.notNull = self.data(self.index(row, 2), Qt.CheckStateRole) == Qt.Unchecked

Actually, the data in column 2 (notNull) doesn't have a Qt.CheckStateRole value (None), so the test is always false.

Furthermore, the notNull column should be a checkboxed column and it only appears as a standard column (with True or False strings).

By adding the two following lines in the append method of the TableFieldsModel class from db_plugins/data_model.py, I think that the bug is fixed.

...
class TableFieldsModel(SimpleTableModel):
...
    def append(self, fld):
        data = [fld.name, fld.type2String(), not fld.notNull, fld.default2String()]
        self.appendRow(self.rowFromData(data))
        row = self.rowCount() - 1
        self.setData(self.index(row, 0), fld, Qt.UserRole)
        self.setData(self.index(row, 1), fld.primaryKey, Qt.UserRole)
        self.setData(self.index(row, 2), not fld.notNull, Qt.CheckStateRole) # set check state
        self.setData(self.index(row, 2), None, Qt.DisplayRole) # don't show the value
...

Whith those two lines, the edit field dialog finds the good value for notNull variable and user can check the box directly inside the column.

Thanks for fixing this...

DBManager_not_null_checkbox_edit_field.png - With patch (29.7 KB) Médéric RIBREUX, 2015-07-08 05:31 AM

Associated revisions

Revision a2ce73ae
Added by Giuseppe Sucameli over 9 years ago

[DBManager] retrieve and store columns not null value when editing table (fix #13089)

History

#1 Updated by Giovanni Manghi over 9 years ago

  • Target version deleted (Version 2.12)
  • Status changed from Open to Feedback

Well... thank you for the patch suggestion. May I ask you to make a Pull Request on QGIS Github repository? Thanks

https://github.com/qgis/QGIS

#2 Updated by Giuseppe Sucameli over 9 years ago

  • Status changed from Feedback to Closed

Also available in: Atom PDF