Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to restore mysql backup that have generated always as column?

CREATE TABLE `revenue_daily` ( `wallet` varbinary(100) NOT NULL DEFAULT '0',
`tc_access` varbinary(100) NOT NULL DEFAULT '0',
`tc_short` varbinary(100) NOT NULL DEFAULT '0',
`total_toll_collection` varbinary(100) GENERATED ALWAYS AS (`wallet` + `tc_access`) VIRTUAL NOT NULL,
`cash_collection` varbinary(100) GENERATED ALWAYS AS (`total_toll_collection` - `tc_short`) VIRTUAL NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=ascii;

That table has generated column.I backed up database structure with data and when i am restoring same .sql file then error occur. Error is:-

ERROR 3105 (HY000) at line 262: The value specified for generated column 'total_toll_collection' in table 'revenue_daily' is not allowed. 

I am using mysql version:-

sunilp@sunilp ~> mysql --version
mysql: [Warning] World-writable config file '/etc/mysql/mysql.conf.d mysqld.cnf' is ignored.
mysql  Ver 14.14 Distrib 5.7.18, for Linux (x86_64) using  EditLine wrapper
like image 607
sunil Avatar asked Sep 09 '17 06:09

sunil


People also ask

How do I restore a backup in MySQL?

In Database Explorer, right-click the server connection on which you want to restore the database and select Backup and Restore > Restore Database. In the Database Restore Wizard, select the backup file and click Restore.

Can I restore a single table from a full MySQL Mysqldump file?

As outlined in the intro, there are a few required steps you need to perform to restore a single table from a mysqldump backup, because all your tables and data are in one file. Your mysqldump backup file might be hundreds MB's in size. Therefore, you first need to single out the table you want restored.

What is generated column in MySQL?

A generated column definition can refer to any base (nongenerated) column in the table whether its definition occurs earlier or later. The AUTO_INCREMENT attribute cannot be used in a generated column definition. An AUTO_INCREMENT column cannot be used as a base column in a generated column definition. As of MySQL 5.7.


1 Answers

This is a problem when using mysqldump from MariaDB with virtual generated columns. MariaDB's mysqldump apparently dumps the generated values, but MySQL only accepts DEFAULT as value for a virtual generated column.

It seems like you need to use MySQL's mysqldump to correctly dump and restore virtual generated columns on a MySQL server.

The bug was also reported here.


What I do as a workaround, is replace the virtual column in the dump:

sed -i 's/GENERATED ALWAYS AS .* VIRTUAL/NOT NULL/' mydump.sql

then restore the dump, then drop/add the generated column again:

mysql -e "ALTER TABLE foo DROP COLUMN bar;\
ALTER TABLE foo ADD COLUMN bar VARCHAR(255) AS ...;"

I also posted this answer here.

like image 99
Dario Seidl Avatar answered Oct 19 '22 07:10

Dario Seidl