Can not drop virtual column | ERROR 1054 (42S22): Unknown column in 'GENERATED ALWAYS'
2
votes
1
answer
2060
views
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
// FINAL
I've tried workaround provided by @RolandoMySQLDBA and it helped after slightly modification.
Command
INSERT INTO some_new_table_name_here SELECT * FROM some_table_name_here;
produced error:
ERROR 1906 (HY000): The value specified for generated column 'calcOne' in table 'some_table_name_here' has been ignored
Since there are generated columns there, it's not possible to INSERT value into that column, so I had to change INSERT query to this:
INSERT INTO some_new_table_name_here (id
, title
, description
, price_actual
, auction
, square_meter_price
, square_meter_price_analog
, accuracy
, accuracy_analog
, total_area
) SELECT id
, title
, description
, price_actual
, auction
, square_meter_price
, square_meter_price_analog
, accuracy
, accuracy_analog
, total_area
FROM some_table_name_here;
And afterwards command was fine too:
ANALYZE TABLE some_new_table_name_here;
+-----------------------------+---------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+-----------------------------+---------+----------+----------+
| db.some_new_table_name_here | analyze | status | OK |
+-----------------------------+---------+----------+----------+
1 row in set (2.137 sec)
I've also discovered one nice thing - after successfully duplicating table, renaming both of old and new of them and doing analyze, I tried to analyze and also optimize some_old_table_name_here
table and it worked without errors!
IDK what was the problem. Maybe since the table previously was in use, it was not possible to alter it, while there are generated columns.
Asked by iorsa
(31 rep)
Jun 13, 2021, 11:11 AM
Last activity: Jun 14, 2021, 10:26 AM
Last activity: Jun 14, 2021, 10:26 AM