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

Harry Sfougaris hsfougaris at gmail.com
Fri May 14 06:51:43 CEST 2010


I found a semi-acceptable solution for me:
I wired the beforeInsert signal of my model to a slot, where I do the following

    if (record.isNull("myFld1")) record.setGenerated("myFld1", false);

I don't know if there is a more clever way, but at least this works!

Harry




On 14 Μαϊ 2010, at 6:03 π.μ., Harry Sfougaris wrote:

> Thanks for that. 
> So, i guess there is also no flag or option in a mapper to only generate sql statements for fields that have actually changed? 
> I am trying to avoid hardcoding my default values in the app. I wonder if in the api you can at least get the default values from the database. 
> 
> 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. 
> > 
> > 
> > _______________________________________________ 
> > Qt-interest mailing list 
> > Qt-interest at trolltech.com 
> > http://lists.trolltech.com/mailman/listinfo/qt-interest 
> 
> 

Harry Sfougaris




-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100514/25779c6c/attachment.html 


More information about the Qt-interest-old mailing list