3

I´m still in shock with what happened to me yesterday. I have a mysql database running in production. Yesterday trying to fix a performance issue in our software, I figured out that we were missing a a foreign key in a table. I run this alter table (IN PRODUCTION) using workbench:

ALTER TABLE discounts ADD INDEX (product_id),
                     ADD FOREIGN KEY (product_id) REFERENCES products (id);

Surprisingly, I got a connection lost error, and to my surprise the whole table discounts has been dropped. Lucky I got a backup, but now I´m pretty scared.

I´m trying to re create the table:

, but I´m getting Error Code: 1005. Can't create table 'yanpyprod.boats_discounts' (errno: 150). I have read other posts such us MySQL: Can't create table (errno: 150) but just for testing, even if I try to create next table without any foreign keys, I´m getting the error.

 CREATE TABLE discounts (
  id INTEGER  NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (id)
);

UPDATE: To answer @Eden comment, I have exactly the same database replicated in development and I can create the table without problems.

UPDATE 2: For debugging purposes, I run.

SET FOREIGN_KEY_CHECKS=0;
I DROP TABLE boats_discounts;
CREATE TABLE boats_discounts (
  id INTEGER  NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (id)
);

In development, all three queries worked perfectly. In production, I got: Error Code: 1051. Unknown table 'boats_discounts' when tried to drop the table. - Error Code: 1005. Can't create table 'yanpyprod.boats_discounts' (errno: 150) when tried to create it.

UPDATE 3:

I have run next query to find out if there is any foreign key referencing table discounts. I can confirm that there isn´t any foreign key to discounts.id

SELECT
TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME
FROM
INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE
REFERENCED_TABLE_SCHEMA = 'prod' AND
REFERENCED_TABLE_NAME = 'discounts';

UPDATE 4: After running this SHOW ENGINE INNODB STATUS; that I read here: Error Code: 1005. Can't create table '...' (errno: 150) I can see this interesting information error. It´s totally related, but still doesn´t make sense to me. I mean,

LATEST FOREIGN KEY ERROR

200226 16:21:14 Error in foreign key constraint of table schema/discounts: there is no index in the table which would contain the columns as the first columns, or the data types in the table do not match the ones in the referenced table or one of the ON ... SET NULL columns is declared NOT NULL. Constraint: , CONSTRAINT "discounts_ibfk_1" FOREIGN KEY ("discount_type_id") REFERENCES "discount_types" ("id")

This is the discount_types DDL:

CREATE TABLE `discount_types` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(60) NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8

But, using this query that I found here: List of Constraints from MySQL Database I cannot find the 'discounts_ibfk_1' foreign key constraint:

select *
from information_schema.table_constraints
where constraint_schema = 'yanpyprod'

UPDATE 5: I´m desperate. I´m running all queries in MySQL workbench. When I try to run the same query:

CREATE TABLE discounts (
      id INTEGER  NOT NULL AUTO_INCREMENT,
      PRIMARY KEY (id)
    );

directly in mysql console, I get a different error: ERROR 1146 (42S02): Table 'discounts' doesn't exist

It´s pretty clear that the table doesn´t exist, because I´m trying to create it. So, I think something got corrupted. However, no idea how to fix it.

Please also see this example. Note that I have just created table discounts2 exactly as discounts, then created the indexes and foreign keys and then tried to rename discounts2 to discounts. Please see the result:

mysql> drop table discounts;
ERROR 1051 (42S02): Unknown table 'discounts'
mysql> select * from discounts;
ERROR 1146 (42S02): Table 'discounts' doesn't exist
mysql> ALTER TABLE discounts2 RENAME TO  discounts;
ERROR 1050 (42S01): Table './schema/discounts' already exists

Again, I would say the database is corrupted, but I don´t know how to fix this.

Rober
  • 5,868
  • 17
  • 58
  • 110
  • Do you have any other tables that have foreign keys on the discounts.id? For example you might have an orders table with a foreign key column discount_id. Is this the case? – Eden Dowling-Mitchell Feb 26 '20 at 10:08
  • No. Please check my update. – Rober Feb 26 '20 at 10:46
  • when you say you have the same database replicated in dev, does that include all the same data, same schema, and same global variables? If so, then i'm stumped. It sounds like `id INTEGER NOT NULL AUTO_INCREMENT` should possibly be `id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT` to match a foreign key column that exists on another table. OR some other table is empty on dev and is not empty on prod. But if you have a replica, including all the data and settings, then it can't be those :) – Eden Dowling-Mitchell Feb 26 '20 at 12:01
  • If you can be bothered sharing more about your database structure I may be able to help. It would also be interesting to see if the problem persists if you first run SET FOREIGN_KEY_CHECKS=0; and then run the create table query. This would probably just point to another issue but would at least help you troubleshoot. – Eden Dowling-Mitchell Feb 26 '20 at 12:03
  • Thanks for your help! If you can be more specific about the database structure information that you need, I will try to provide it. Initially the tables structure in dev and prod is the same. The data is totally different. There is just a bunch of rows in development. – Rober Feb 26 '20 at 12:22
  • Please read my new comments. – Rober Feb 26 '20 at 12:24
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/208570/discussion-between-eden-dowling-mitchell-and-rober). – Eden Dowling-Mitchell Feb 26 '20 at 12:40
  • So you tried to add a foreign key to `discounts` the table `discounts` got delete / disappeared, you tried to create the table `discounts` but your error message said the table `boats_discounts` could not be created? – SE1986 Feb 26 '20 at 12:46
  • Yes. This is right! – Rober Feb 26 '20 at 13:28

0 Answers0