1

I'm trying to drop a foreign key column in a table I have named ProductInvoice. The column I'm trying to drop is named PersonID and is from the table Person. When I run the query

ALTER TABLE ProductInvoice
DROP COLUMN PersonID;

I get this error...

Error Code: 1025. Error on rename of './jkripal/#sql-91c_19ff0' to './jkripal/ProductInvoice' (errno: 150)

Any advice on how to troubleshoot this? I've looked around this site and can't find any answers that help.

These are the results from SHOW CREATE TABLE ProductInvoice

'ProductInvoice', 'CREATE TABLE `ProductInvoice` 
(\n  `ProductInvoiceID` int(11) NOT NULL AUTO_INCREMENT,
\n  `PersonID` int(11) DEFAULT NULL,
\n  `ProductID` int(11) NOT NULL,
\n  `InvoiceID` int(11) NOT NULL,
\n  `TravelDate` varchar(255) DEFAULT NULL,
\n  `TicketNote` varchar(255) DEFAULT NULL,
\n  `Quantity` int(11) DEFAULT NULL,
\n  `InsuranceTicketCode` varchar(255) DEFAULT NULL,
\n  PRIMARY KEY (`ProductInvoiceID`),
\n  KEY `fkPerson` (`PersonID`),
\n  KEY `fk_ProductInvoice_to_Product` (`ProductID`),
\n  KEY `fk_ProductInvoice_to_Invoice` (`InvoiceID`),
\n  CONSTRAINT `ProductInvoice_ibfk_1` FOREIGN KEY (`PersonID`) REFERENCES `Person` (`PersonID`),
\n  CONSTRAINT `ProductInvoice_ibfk_2` FOREIGN KEY (`ProductID`) REFERENCES `Product` (`ProductID`),
\n  CONSTRAINT `ProductInvoice_ibfk_3` FOREIGN KEY (`InvoiceID`) REFERENCES `Invoice` (`InvoiceID`)
\n) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8'
Mihai
  • 26,325
  • 7
  • 66
  • 81
Ryan Dorman
  • 317
  • 1
  • 3
  • 10

2 Answers2

1

First disable foreign keys

SET foreign_key_checks = 0;

Delete the key on PersonID

ALTER TABLE ProductInvoice
  DROP INDEX fkPerson;

Now drop the foreign key

ALTER TABLE ProductInvoice DROP FOREIGN KEY ProductInvoice_ibfk_1;

Drop the column

ALTER TABLE ProductInvoice DROP COLUMN PersonID;

Enable foreign keys:

SET foreign_key_checks = 1;
Mihai
  • 26,325
  • 7
  • 66
  • 81
0

Drop Foreign key first:

ALTER TABLE ProductInvoice DROP FOREIGN KEY ....;

and then:

ALTER TABLE ProductInvoice DROP COLUMN PersonID;
Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275