Page 1 of 1

Required is true for calculate field?

Posted: 18.09.2006, 09:40
by dhongu
I have mysql table
[syntax="sql"]
CREATE TABLE `tabel1` (
`ID` int(11) NOT NULL auto_increment,
`Val` int(11) default NULL,
`Description` varchar(20) default NULL,
PRIMARY KEY (`ID`)
) TYPE=MyISAM;
[/syntax]

and code:
[syntax="delphi"]
procedure TForm1.Button1Click(Sender: TObject);
var MyZQuery:TZQuery;
begin
MyZQuery:=TZQuery.Create(self);
MyZQuery.Connection:=ZConnection1;
MyZQuery.SQL.Text:='select *, id+1 as Calc from tabel1';
MyZQuery.Open;
MyZQuery.Insert;
MyZQuery.FieldByName('Val').Value:='1000';
MyZQuery.FieldByName('Description').Value:='Description 1000';
MyZQuery.Post;
MyZQuery.Close;
MyZQuery.Free;
end;
[/syntax]

If you try to post on new record without set field `calc` program raised exception class EDatabaseError with message 'Field 'Calc' must have a value'.

For simple sql with calculate field is necessary to define ZUpdateObject :(.
It is possible to set Request = false for calculate field ?

Posted: 05.03.2007, 12:10
by dhongu
?

Posted: 05.03.2007, 14:38
by btrewern
Try setting

Code: Select all

MyZQuery.FieldByName('Calc').Required := False;
Not tested but should work.

Regards,

Ben

Posted: 07.03.2007, 17:02
by mdaems
Hi Dorin,

I just added a small test to avoid that fields which are not writable are set required.
For me it seems to work right.
Real reason for the error : mysql returns these fields as not null (field flags in C api) Apparently it derives this from the fact that the id column is not null and it's just an addition. If your calculation would have been more difficult like 'if(id<10,null,id) as calc' the api returns 'null allowed' and the required property is not set.

Committed in SVN testing branch REV 226.

Mark