You will need to first remove or update some rows in table user_surveys_archive
.
Those rows are related to rows in the user_surveys
table.
Likely, there's a foreign key constraint defined on table user_surveys
that references rows in surveys
you are attempting to delete.
(You'd need to check the foreign key definition, quickest way to get that is a
SHOW CREATE TABLE user_surveys
And look for REFERENCES surveys
. (Likely, its a column named survey_id
, but we're just guessing without looking at the definitions of the foreign key constraints.)
To find the rows in user_surveys_archive
that are preventing the DELETE from happening...
SELECT a.*
FROM user_surveys_archvive a
JOIN user_surveys u
ON u.user_access_level_id = a.user_access_level_id
JOIN surveys s
ON s.survey_id = u.survey_id
WHERE s.survey_id = 77
It's likely that the foreign key constraint from user_surveys
to surveys
is defined with ON DELETE CASCADE
. The attempt to delete rows from surveys
identifies rows in user_surveys
that should automatically be removed.
The attempt to automatically remove the rows from user_surveys
is what's violating the foreign key constraint defined in user_surveys_archive
. And that foreign key is not defined with ON DELETE CASCADE
.
(The other possibility is that there's a trigger defined that's doing some DML operations, but that would be odd.)
Once you identify the rows, you need to decide what changes to make to allow you to remove rows from surveys
.
You can either DELETE the rows, or UPDATE them.
To delete the rows from user_surveys_archive
, modify the query above and replace SELECT
with DELETE
. If the user_access_level_id
column in user_surveys_archive
allows for NULL values, you can do an update.
Replace SELECT a.* FROM
with UPDATE
, and add SET a.user_access_level_id = NULL
on a line above the WHERE
clause...
UPDATE user_surveys_archvive a
JOIN user_surveys u
ON u.user_access_level_id = a.user_access_level_id
JOIN surveys s
ON s.survey_id = u.survey_id
SET a.user_access_level_id = NULL
WHERE s.survey_id = 77
(It seems strange to me that name of the foreign key column is user_access_level_id
. But its just a column name, it could be named anything... seems odd to me because of the conventions and patterns that we follow in naming foreign key columns.)
user_surveys_archive_ibfk_6
is the name of the foreign key constraint, and generally can be absolutely anything you want - it's just a name, after all. it looks like an auto-generated one, so it's most likely in theuser_surveys_archive
table. – Marc Balter
queries with foreign_key_checks turned off. once the check is re-enabled, mysql won't scan the DB to verify all FKs are working, so you wouldn't have noticed until you tried a query which uses/refers to the now-nonexistent fields/tables. – Marc Bsurveys
is not the issue. You need to look at the foreign key on theuser_surveys
table. We expect that is going to beFOREIGN KEY (survey_id) REFERENCES surveys(survey_id) ON DELETE CASCADE
, as I explained in my answer. – spencer7593