Skip to main content
6 of 7
few additions
iorsa
  • 31
  • 4

Can not drop virtual column | ERROR 1054 (42S22): Unknown column in 'GENERATED ALWAYS'

I'm just running a command: optimize table some_table_name_here;

But it results with:

Table | Op | Msg_type | Msg_text db.some_table_name_here | optimize | note | Table does not support optimize, doing recreate + analyze instead db.some_table_name_here | optimize | error | Unknown column '`db`.`t`.`total_area`' in 'GENERATED ALWAYS' db.some_table_name_here | optimize | status | Operation failed 3 rows in set, 1 warning (0.001 sec) 

Server version: 10.5.10-MariaDB-1:10.5.10+maria~buster-log mariadb.org binary distribution.

This problem appeared after upgrade from mariadb 10.3 to 10.5 (via 10.4).

// EDIT

I've recently found that the problem is related to generated (virtual) persistent/stored columns that I have in this table.

Here are both columns that may have problems:

`calcOne` float GENERATED ALWAYS AS (if(`total_area` = 0,0,if(`square_meter_price` = 0,0,round(((`price_actual` - `auction`) / `total_area` / `square_meter_price` - 1) * 100,2)))) STORED `calcTwo` float GENERATED ALWAYS AS (if(`total_area` = 0,0,if(`square_meter_price_analog` = 0,0,round(((`price_actual` - `auction`) / `total_area` / `square_meter_price_analog` - 1) * 100,2)))) STORED 

I've already tried ALTER TABLE some_table_name_here DROP COLUMN calcOne; to drop column and add it again, but error happens: ERROR 1054 (42S22): Unknown column '`db`.`t`.`total_area`' in 'GENERATED ALWAYS'

// EDIT

As requested (result of SHOW CREATE TABLE some_table_name_here\G):

CREATE TABLE `some_table_name_here` ( `id` bigint(11) NOT NULL, `title` varchar(1000) CHARACTER SET utf8mb4 DEFAULT NULL, `description` text CHARACTER SET utf8mb4 NOT NULL, `price_actual` int(15) NOT NULL, `auction` int(11) DEFAULT 0, `square_meter_price` int(11) DEFAULT 0, `square_meter_price_analog` int(11) DEFAULT 0, `accuracy` int(11) DEFAULT 0, `accuracy_analog` int(11) DEFAULT 0, `total_area` float DEFAULT 0, `calcOne` float GENERATED ALWAYS AS (if(`total_area` = 0,0,if(`square_meter_price` = 0,0,round(((`price_actual` - `auction`) / `total_area` / `square_meter_price` - 1) * 100,2)))) STORED, `calcTwo` float GENERATED ALWAYS AS (if(`total_area` = 0,0,if(`square_meter_price_analog` = 0,0,round(((`price_actual` - `auction`) / `total_area` / `square_meter_price_analog` - 1) * 100,2)))) STORED, PRIMARY KEY (`id`) USING BTREE, KEY `idx_price_actual` (`price_actual`), KEY `idx_auction` (`auction`), KEY `idx_square_meter_price` (`square_meter_price`), KEY `idx_square_meter_price_analog` (`square_meter_price_analog`), KEY `idx_accuracy` (`accuracy`), KEY `idx_accuracy_analog` (`accuracy_analog`), KEY `idx_total_area` (`total_area`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 

// EDIT

When I added those virtual (generated) columns (on original mariadb server 10.3.27) I used this command/query:

ALTER TABLE some_table_name_here ADD COLUMN `sootn` float AS (IF(total_area=0, 0, IF(square_meter_price=0, 0, ROUND(((((price_actual-auction)/total_area/square_meter_price)-1)*100), 2)))) PERSISTENT; ALTER TABLE some_table_name_here ADD COLUMN `sootn_analog` float AS (IF(total_area=0, 0, IF(square_meter_price_analog=0, 0, ROUND(((((price_actual-auction)/total_area/square_meter_price_analog)-1)*100), 2)))) PERSISTENT; 

There were no such problems before. I used optimize table command in order to free space from unexisting/dropped indexes. I've upgraded mariadb 100% using this instructions: https://mariadb.com/kb/en/upgrading-from-mariadb-103-to-mariadb-104/

https://mariadb.com/kb/en/upgrading-from-mariadb-104-to-mariadb-105/

https://mariadb.com/docs/deploy/upgrade-community-server/

Is there any way how can I find where and why this problem occurs?

For now I only see this way in fixing this problem:

  1. dump/export table
  2. drop it (if it's possible)
  3. create table structure
  4. fill it with data
iorsa
  • 31
  • 4