0

I have two tables: parent and child. One of the child column references parent PRIMARY KEY with FOREIGN KEY constraint - child.parent_id references parent.parent_id.

Foreign key column in child tables allows NULLs values because when child will be created parent may be not known yet. But when info about parent will be assigned/updated i want to have referential integrity.

And now the question:

How to pass null value from HTML form to MySQL? Quotes '' or "" not working "foreign key constraint fails" probably because it passes empty string instead of null value. Or maybe do I need to do some additional checking in PHP and convert it to null ?

CREATE TABLE IF NOT EXISTS `parent` (
  `parent_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL,
  `surname` varchar(20) NOT NULL,
  PRIMARY KEY (`parent_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `child` (
 `child_id` int(11) NOT NULL AUTO_INCREMENT,
 `child_name` varchar(20) NOT NULL,
 `parent_id` int(11) DEFAULT NULL,
 PRIMARY KEY (`child_id`),
 KEY `fk_parent_id` (`parent_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

ALTER TABLE `child`
  ADD CONSTRAINT `fk_parent_id` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`parent_id`);

2 Answers 2

0

You need to convert your empty value to null or edit your query in PHP.

Sign up to request clarification or add additional context in comments.

Comments

0

Why do you want to pass up a NULL value when your tables do not support NULL values in the parent? Also

`parent_id` int(11) DEFAULT NULL,

Should be NOT NULL as a child should always have a parent.

If you are trying to pass parent/child identifiers from a HTML form, I would recommend validating them server side in your PHP code. As I could easily manipulate the values in the markup and add any parent/child combination I wish.

5 Comments

I want to allow null value in the child table because the parent may still be unknown during child creation and the relation will be created later. I've omitted validation for sake of simplicity and validation won't convert it.
@czomberzdaniela - you can't do that. You have a foreign key constraint that states that parent_id in the child table must reference an ID in the parent table. If you create a child record without a valid parent id then the constraint will be violated.
Yes You can. INSERT INTO foreign_test.child (child_id, child_name, parent_id) VALUES (NULL, 'Jack', NULL); works fine. You can also do this from phpMyAdmin.
@czomberzdaniela - does foreign test have a foreign key on parent_id that is referencing the parent table?
yes of course: ALTER TABLE child ADD CONSTRAINT fk_parent_id FOREIGN KEY (parent_id) REFERENCES parent (parent_id);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.