4

I try to add a generated stored column to existing table running the following SQL command:

ALTER TABLE routes ADD routes_hash char(32)
      AS (MD5(CONCAT(`traveler_id`, `track`))) stored;

MySQL returns the error message:

ERROR 1025 (HY000): Error on rename of './travelers/#sql-558_69' to './travelers/routes'
         (errno: 150 - Foreign key constraint is incorrectly formed)

Mysql version is 5.7.20. Here is a database structure:

CREATE TABLE `travelers` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(128) NOT NULL,
   PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `routes` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `traveler_id` int(11) NOT NULL,
  `track` multilinestring NOT NULL,
  PRIMARY KEY (`id`),
  KEY `traveler_id` (`traveler_id`),
  CONSTRAINT `routes_ibfk_1` FOREIGN KEY (`traveler_id`)
        REFERENCES `travelers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

I have not found any restrictions in MySQL documentation that forbid usage of the column that participates in foreign key constraint as a part of expression used for build a generated column. However, if I exclude traveler_id from the generated column expression for routes_hash the column is created fine. Is this a bug of MySQL or I am missing something about generated column usage?

Yves M.
  • 29,855
  • 23
  • 108
  • 144
andrew
  • 81
  • 6
  • Adding the generated column apparently leads to the recreation of the entire table and you get the error message when mysql tries to recreate the foreign key. Check innodb status monitor to see the exact error message. – Shadow Jul 27 '18 at 15:38
  • Unfourtunately, 'LATEST FOREIGN KEY ERROR' section lacks in output of `SHOW ENGINE INNODB STATUS` that means that no foreign key error has been detected. – andrew Jul 27 '18 at 16:21
  • 3
    I could reproduce it without any data. For me it looks like a bug. Note that it works, if you remove the `stored` attribute. – Paul Spiegel Jul 27 '18 at 17:08
  • @PaulSpiegel I would venture that without the stored attribute the table does not have to be recreated. – Shadow Jul 27 '18 at 18:48
  • @Shadow I guess you're right. But you will have the same issue with a [single CREATE statement](https://www.db-fiddle.com/f/v7h6dsFr1yWKE5GR2b4i2w/0). I think they have troubles to combine *stored* generated columns with *cascading actions*. A similar problem exists with triggers. They are not fired by cascading actions. – Paul Spiegel Jul 27 '18 at 20:09
  • Note that MariaDB doesn't have this issue. Tested on 10.0.19 and [on 10.2 here](https://dbfiddle.uk/?rdbms=mariadb_10.2&fiddle=8ecd50f8f6cda8d8ad5b7abfeacde501). – Paul Spiegel Jul 27 '18 at 20:10

1 Answers1

3

Unfortunately, it is a bug of MySQL that affects even current 5.7 and 8.0 versions. I have found description on MySQL's bug tracker https://bugs.mysql.com/bug.php?id=88111 . Please be aware.

andrew
  • 81
  • 6
  • Thanks for the info, that was driving me crazy! As a workaround I'm using `VIRTUAL` instead of `STORED`. – Yves M. Mar 08 '21 at 16:44