I have a MySQL database for tbl_image and tbl_product_index.When I am going to upload image in image model, everything is fine here but when I am going to upload image in image model it is showing error like this:
"CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (technoc1_notjustprice
.tbl_image
, CONSTRAINT tbl_image_ibfk_2
FOREIGN KEY (product_id
) REFERENCES tbl_product_index
(product_id
) ON DELETE CASCADE). The SQL statement executed was: INSERT INTO tbl_image
(product_id
) VALUES (:yp0)"
I don’t know why it is happening here.Here is my database tables
Table structure for table tbl_image
–
CREATE TABLE IF NOT EXISTS tbl_image
(
product_id
varchar(30) NOT NULL,
image_index
varchar(20) NOT NULL,
image_location
varchar(50) NOT NULL,
PRIMARY KEY (image_index
),
KEY product_id
(product_id
),
KEY image_location
(image_location
),
KEY image_index
(image_index
),
KEY image_index_2
(image_index
)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
–
– Constraints for dumped tables
–
–
– Constraints for table tbl_image
–
ALTER TABLE tbl_image
ADD CONSTRAINT tbl_image_ibfk_2
FOREIGN KEY (product_id
) REFERENCES tbl_product_index
(product_id
) ON DELETE CASCADE;
and
Table structure for table tbl_product_index
–
CREATE TABLE IF NOT EXISTS tbl_product_index
(
index
int(11) NOT NULL AUTO_INCREMENT,
product_name
varchar(30) NOT NULL,
product_id
varchar(30) NOT NULL,
master_key
int(11) NOT NULL,
PRIMARY KEY (product_id
),
UNIQUE KEY master_key
(master_key
),
KEY index
(index
)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
–
– Dumping data for table tbl_product_index
–
INSERT INTO tbl_product_index
(index
, product_name
, product_id
, master_key
) VALUES
(1, ‘micromax A100’, ‘mxn’, 1),
(2, ‘micromax A10’, ‘mxn10’, 2);
–
– Constraints for dumped tables
–
–
– Constraints for table tbl_product_index
–
ALTER TABLE tbl_product_index
ADD CONSTRAINT tbl_product_index_ibfk_1
FOREIGN KEY (master_key
) REFERENCES tbl_master_index
(master_key
) ON DELETE CASCADE;
any help…below in files model,controller and view code for the image
please help me.it is very important for me…
thanks in advance…