[Qt-interest] QSqlTableModel NOT NULL constraints and default values
Harry Sfougaris
hsfougaris at gmail.com
Sat May 15 07:19:28 CEST 2010
>>
>
> You can use the DEFAULT keyword.
> INSERT INTO tableXXX (col1, col2) VALUES (DEFAULT, 'other value');
> or
> UPDATE tableXXX SET col1 = DEFAULT, col2 = 'new other value' WHERE cond;
>
But I am using an QSqlTableModel - I am not generating any SQL myself.
Harry
>>
>> ----- Original message -----
>>
>>>> -----Original Message-----
>>>> Hi all,
>>>> I am using a Postgres table that has some fields defined as
>>>> ....
>>>>
>>>> myFld1 character varying(1024) NOT NULL DEFAULT 'my default
>>>>
>>>> value'::character varying,
>>>> ...
>>>> I am using a QDataWidgetMapper with a delegate that converts
>>>> the data in empty text boxes to NULL instead of ''
>>>> This works with fine with other fields that don't have a NOT
>>>> NULL constraint.
>>>>
>>>> When I am trying to use it with a field that has a NOT NULL
>>>> constraint though, upon submit() I get the error:
>>>> "ERROR: null value in column "myFld1" violates not-null
>>>> constraint. QPSQL: unable to create query"
>>>>
>>>> Is there a flag I can set so that the model will allow the
>>>> query to go to the server, or another solution?
>>>>
>>>> Thanks,
>>>
>>> Hi Harry,
>>>
>>> I haven't used Postgres, but Oracle, Firebird and MSSQL. In all of those
>>> three, sending NULL to a NOT NULL column will cause a server error. The
>>> DEFAULT clause is only used when the insert statement does not include
>>> the column. If you explicitly or implicitly include the column name,
>>> then you must provide a non NULL value. I'm not sure whether the Qt
>>> drivers provide the field's default value for you - that's probably too
>>> DBMS specific. So you may need to set that default in your delegate
>>> yourself for these fields, instead of NULL.
>>>
>>> Hope that helps,
>>>
>>> Tony.
>
> --
> Robert Voinea
> IT Specialist
> +4 0740 467 262
> rvoinea (at) gmail [dot] com
Harry Sfougaris
More information about the Qt-interest-old
mailing list