Learn how to fix the AttributeError("module 'string' has no attribute 'maketrans'") that appears when migrating a database with MySQL Workbench.

How to fix MySQL Workbench 8.0 CE SystemError: AttributeError("module 'string' has no attribute 'maketrans'") error calling Python module function DbMssqlMigration.migrateCatalog

While migrating a SQL Server database to MySQL, I decided to use the awesome MySQL workbench that allows you to migrate easily this type of DB within minutes depending on their size of course. While following all the migration steps as usual, I found a problem that appears when the reverse-engineered object from the source RDBMS is converted to MySQL-compatible objects. The task simply fails to be executed providing the following output:

The problem and solution

The file with the problem is a Python script that is included as a module in the installation directory of MySQL workbench specifically db_mssql_migration_grt.py that helps with the migration, specifically in the line 148:

C:\Program Files\MySQL\MySQL Workbench 8.0 CE\modules\db_mssql_migration_grt.py

The code checks if the name of the database schema and tables match the requirements of not having either dots, slashes, or backslashes (so if for some reason, your schema or tables don't comply with the rules, you will find this exception when using MySQL workbench):

if not dots_allowed and not mysql_valid_regex.match(mysql_name):
    # This is the offending code that throws the exception
    mysql_name = mysql_name.translate(string.maketrans(r'/\.', '___'))
    log_message = ('Schema and table names cannot contain "/", "\\", ".", or characters that are '
                    'not permitted in file names. The identifier [%s] was changed to `%s`. ' % (mssql_name, mysql_name))

The problem with this code is, that since Python 3 the maketrans method from string is not available anymore which is the version that MySQL Workbench 8.0.34 uses internally (Python 3.11.4), this script is not being updated in the latest releases of MySQL Workbench or at least that's what I have found testing MySQL Workbench from v8.0.30 to v8.0.34, specifically when the name of a table doesn't meet the requirements and has to be modified.

Fortunately, the solution to this exception is actually simple and you will be able to migrate as usual within a few seconds. First, close any open instance of MySQL Workbench. Now open the db_mssql_migration_grt.py file in your favorite code editor or with any text editor and the only thing you need to do is to replace any occurrence of string.maketrans in the file (actually, there's only one occurrence in the line 128):

string.maketrans(r'/\.', '___')

Replace it with str.maketrans:

str.maketrans(r'/\.', '___')

The code will look like this after the modification:

if not dots_allowed and not mysql_valid_regex.match(mysql_name):
    # This is the offending code that throws the exception
    mysql_name = mysql_name.translate(str.maketrans(r'/\.', '___'))
    log_message = ('Schema and table names cannot contain "/", "\\", ".", or characters that are '
                    'not permitted in file names. The identifier [%s] was changed to `%s`. ' % (mssql_name, mysql_name))

And that's it! Save the changes, you may need Administrator rights to do that properly and launch MySQL Workbench once again. When you reach again this step, you should be able to complete it without any problem:

Happy database migration ❤️!


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors