This topic is locked

Insert error when leaving optional foreign key relationship empty

7/25/2008 8:27:36 AM
PHPRunner General questions
P
PMunnik author

Hi,
Since this week I am evaluating the PHPRunner tool. I think it works really nice and it could save me a lot of work. But, there is one issue that is starting to be very anoying:
In some tables in my database I use an optional foreign key relation with the same or another table. For example the next table definition (slightly altered to contain only the involved fields):
CREATE TABLE `CatCreator`.`TYPE` (

`TYPE_ID` VARCHAR(64) NOT NULL ,

`MotherType` VARCHAR(64) NULL ,

`Description` VARCHAR(256) NULL ,

PRIMARY KEY (`TYPE_ID`) ,

CONSTRAINT `fk_CLASS_CLASS`

FOREIGN KEY (`MotherType` )

REFERENCES `CatCreator`.`TYPE` (`TYPE_ID` )

ON DELETE NO ACTION

ON UPDATE NO ACTION)

ENGINE = InnoDB;
In this case each type can be derived from another type and inherits some stuff from it's mothertype. MotherType could be NULL. Now I created a user interface in which MotherType is a dropdown box using a look up wizard that references the table TYPE. If I now leave mothertype empty when inserting a new type using the generated UI, I get an SQL error with a foreign key contraint. To my knowledge the INSERT statement should contain NULL for the mothertype and this should not be a problem. Why do I get this SQL error? And how can I avoid this?
Thank you in advance.
Kind regards,

Patrick

J
Jane 7/25/2008

Patrick,
lookup wizard inserts empty string if you don't select any value in the dropdown.

To insert NUll value instead add following code to the Before record added event on the Events tab:

if ($values["FieldName"]=="")

unset($values["FieldName"]);

P
PMunnik author 7/25/2008

Great! This is does the trick indeed.
Thnx.