[Qt-interest] QSqlTableModel NOT NULL constraints and default values

Tony Rietwyk tony.rietwyk at rightsoft.com.au
Fri May 14 01:48:13 CEST 2010


> -----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.





More information about the Qt-interest-old mailing list