[Qt-interest] QSqlTableModel NOT NULL constraints and default values
Robert Voinea
rvoinea at gmail.com
Fri May 14 05:24:08 CEST 2010
On Friday 14 May 2010 06:03:29 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
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;
>
> ----- 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
More information about the Qt-interest-old
mailing list