Can a foreign key be NULL? – Databases – SitePoint Forums

0

I have

I set the default foreign key to NULL (to be set later by me)

CREATE TABLE power_panels (
   power_panel_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
   pdu_id TINYINT UNSIGNED DEFAULT NULL,

I get this error
Deploy
(
[Name] => fgy
[Manufacturer] => Manufacturer test
[Model] => Trial model
[Phase] => 1
[Slots] => 2
[Columns] => 2
[pdu_id] =>
[Width] => 3
[Height] => 4
[Voltage] => 6
[Notes] =>
)
INSERT INTO power_panels (name,pdu_id,manufacturer,model,slots,columns,width,height,voltage,phase,createdby,created_date,notes) VALUES ( ‘fgy’,0,’Test Manufacturer’,’Test Model’,2, 2,3,4,’6′,1,’[email protected]’,CURRENT_TIMESTAMP,”)ERROR: Could not perform INSERT INTO power_panels(name,pdu_id,manufacturer,model,slots,columns,width,height , voltage, phase, created_by, date_created, notes ) VALUES ( ‘fgy’,0,’Test Manufacturer’,’Test Model’,2,2,3,4,’6′,1,’[email protected]’ , CURRENT_TIMESTAMP,”). SQLSTATE[23000]: Integrity constraint violation : 1452 Unable to add or update a child row: a foreign key constraint fails (dct.power_panelsCONSTRAINT power_panels_ibfk_1 FOREIGN KEY (pdu_id) REFERENCES pdus (pdu_id))
you can see, pdu_id is not defined,
Can the foreign key be NULL?

Share.

Comments are closed.