'MySQL getting always error 1451 on update a single value in a table
I need a very little help.... I have two tables A and B where B contains a field that is in constraint key with the ID field of table A. Since I needed to update for a record of A the ID taken from another database, equal to the one I am working on, but with the ID in question "correct", so I did:
- dropped the foreign key of table B on that field referenced to the ID of A
- updated at all records on the old value of the field in B with the new value of reference ID where that field of B = to the old value
- then I update the ID of table A with the new value and I get back error 1451 of foreign key on that field of B that I had already updated without problems even if I had dropped the foreign key in B already;
- then I do SET FOREIGN_KEY_CHECKS = 0;
- I try to update the ID of table A for that record with the new value but I always get error 1451 .....
since I dropped the foreign key and did the set as well, is there something else or command that I have to consider? thanks everyone in advance to all!
EDIT:
precisely I followed like this scenario:
CREATE TABLE sample_A (
ID bigint(20) UNSIGNED NOT NULL,
product varchar(60) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE sample_B (
ID bigint(20) UNSIGNED NOT NULL,
ref_id_sample_A_id bigint(20) UNSIGNED NOT NULL,
document_name varchar(300) COLLATE utf8mb4_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
ALTER TABLE sample_A
ADD PRIMARY KEY ( ID )
ALTER TABLE sample_A
MODIFY ID bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=211388375;
COMMIT;
ALTER TABLE sample_B
ADD PRIMARY KEY ( ID );
ALTER TABLE sample_B
MODIFY ID bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=158;
ALTER TABLE sample_B
ADD CONSTRAINT fk_sample_A_sample_B_ref_id FOREIGN KEY ( ref_id_sample_A_id ) REFERENCES sample_A ( ID );
COMMIT;
INSERT INTO sample_A (ID, product) VALUES
(1, 'product 1'),
(211388370, 'product 2'),
(211388371, 'product 3'),
(211388372, 'product 4'),
(211388373, 'product 5');
INSERT INTO sample_B (ID, ref_id_sample_A_id, document_name) VALUES
(1, 211388372, 'document 1'),
(2, 211388371, 'document 2'),
(3, 1, 'document 3'),
(4, 211388373, 'document 4'),
(5, 1, 'document 5'),
(6, 211388370, 'document 6'),
(7, 211388371, 'document 7');
via phpMyAdmin , dropped foreign key fk_sample_A_sample_B_ref_id
UPDATE sample_B SET ref_id_sample_A_id = 211388369 WHERE ref_id_sample_A_id = 1; <---- OK
UPDATE sample_A SET ID = 211388369 WHERE ID = 1; <---- error 1451
SET FOREIGN_KEY_CHECKS = 0;
UPDATE sample_A SET ID = 211388369 WHERE ID = 1; <---- error 1451 again
next operation would be ALTER TABLE sample_B ADD CONSTRAINT fk_sample_A_sample_B_ref_id FOREIGN KEY ( ref_id_sample_A_id ) REFERENCES sample_A ( ID ); to restore foreign key
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
