What could cause a foreign key to not be able to be dropped even when foreign_key_checks = 0?

StackOverflow https://stackoverflow.com/questions/8737929

  •  14-04-2021
  •  | 
  •  

Question

I'm attempting to rebuild my database, but I'm unable to get past dropping any foreign keys from tables, even though I also call SET foreign_key_checks = 0; From the MySQL docs, it seems that's all that I should need to do. What else might I need to do?

SET foreign_key_checks=0;
alter table galleries drop foreign key fk_page_gallery ;
alter table photos drop foreign key fk_photo_gallery ;

create table galleries (
   id          int(11) auto_increment not null  ,
   page_id     int(11)                          ,
   cover_id    int                    null      ,
   title       varchar(1024)                    ,
   slug        varchar(1024)          not null  ,
   description text                   null      ,
   sort_order  int(11)                          ,
   published   tinyint(1)              default 0,
   created     varchar(20)                      ,
   modified    datetime                         ,
   constraint pk_galleries primary key (id)
)   ENGINE=InnoDB DEFAULT CHARSET=latin1;

create table pages (
   id                int(11) auto_increment not null  ,
   menu_id           int(11)                not null  ,
   title             varchar(1024)          not null  ,
       slug              varchar(1024)          not null  ,
   body              text                   not null  ,
   short_description varchar(1024)                    ,
   published         tinyint(1)              default 0,
   created           datetime                         ,
   modified          datetime                         ,
   constraint pk_pages primary key (id)
)   ENGINE=InnoDB DEFAULT CHARSET=latin1;

SET foreign_key_checks=1;

And for reference:

mysql> SHOW CREATE TABLE galleries;
+-----------+-------------------------------------------+
| Table     | Create Table                              |
+-----------+-------------------------------------------+
| galleries | CREATE TABLE `galleries` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `page_id` int(11) DEFAULT NULL,
  `cover_id` int(11) DEFAULT NULL,
  `title` varchar(1024) DEFAULT NULL,
  `slug` varchar(1024) NOT NULL,
  `description` text,
  `sort_order` int(11) DEFAULT NULL,
  `published` tinyint(1) DEFAULT '0',
  `created` varchar(20) DEFAULT NULL,
  `modified` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_page_gallery` (`page_id`),
  KEY `fk_gallery_cover` (`cover_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 |
+-----------+-------------------------------------------+
1 row in set (0.00 sec)

mysql> SHOW CREATE TABLE pages;
+-------+-----------------------------------------------+
| Table | Create Table                                  |
+-------+-----------------------------------------------+
| pages | CREATE TABLE `pages` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `menu_id` int(11) NOT NULL,
  `title` varchar(1024) NOT NULL,
  `slug` varchar(1024) NOT NULL,
  `body` text NOT NULL,
  `short_description` varchar(1024) DEFAULT NULL,
  `published` tinyint(1) DEFAULT '0',
  `created` datetime DEFAULT NULL,
  `modified` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_pages_menu` (`menu_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1 |
+-------+-----------------------------------------------+
Was it helpful?

Solution

The foreign keys in your create table statements are different than those in your drop statements. What's more is that your table definitions don't seem to have foreign keys at all. MySQL may throw a weird error if you try to drop a foreign key that is not there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top