Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

NOT NULL constraint with existing "defaultValue"



  • Hi all.
    I have next record: QSqlField("safe", bool, tableName: "servers", required: yes, generated: yes, defaultValue: "QVariant(QString, "0")", autoValue: false, readOnly: false) "false"
    When i'm tyring to submiAll() i've got an error: "NOT NULL constraint failed: servers.safe Unable to fetch row". It's normally, becouse we have a constraint, but why "defaultValue" not working even my "field" is NULL? How to mark this field as unsafe to avoid checking it for required?



  • Thnx!
    I found a solution myself: model.removeColumn(model.fieldIndex(attribute));
    So sadly that the Qt can't working with "defaults", but SQL can.



  • @exru
    If safe is of type bool, how is QVariant(QString, "0") a suitable value?



  • Because i'm using SQLITE3 databse, where bool = int in fact.



  • @exru
    But you're passing a string! Try a bool or an int!



  • Thnx!
    I found a solution myself: model.removeColumn(model.fieldIndex(attribute));
    So sadly that the Qt can't working with "defaults", but SQL can.



  • @exru

    So sadly that the Qt can't working with "defaults", but SQL can.

    How do you reach that conclusion, given that you have never passed the right value type for your default to Qt? How can removing a column from the model possibly be a "solution" which addresses this correctly?



  • @jonb The QSqlField generated automatically. I don't wanna dig depper into the qt & don't wanna know how he does that. I have another reach experience working with other languages where model behave themself as expected. And it's not mine error where i wrote wrong SQL table or query. In fact, if you make a query like this: insert into table (name) values ("123"); // and it's working correctly, despite existing NOT NULL constraint in another field, why Qt can't do the same?



  • @exru said in NOT NULL constraint with existing "defaultValue":

    insert into table (name) values ("123"); // and it's working correctly, despite existing NOT NULL constraint in another field, why Qt can't do the same?

    because for a multi-column table where some columns have default values, the above insertion is ambiguous, and probably a violation of the SQL standard (even if some DBMS cheat and allow it). If you are not supplying values for ALL the columns in your insert then you must name each column that you are not using a default value for.



  • @kent-dorfman thank you!

    some DBMS cheat and allow it...
    You a little wrong thinking this is "cheat". I don't know DBMS which not to do that. At least: MySql, PostgreSQL, SQLITE 100% use that mech.


Log in to reply