MySQL Workbench: Script for adding columns to all tables in a model

Here’s a quick Python script for adding columns for all tables in your EER model. This will create a create_time and update_time columns in all tables, but you can modify it to your needs. To execute:

  1. go to Scripting -> Scripting Shell…
  2. click the New Script toolbar icon at the top left corner
  3. select Python Script and specify some name for the script file
  4. click Create
  5. copy/paste the script there
  6. click the Execute toolbar button

Make sure to backup your model before running this!

The code

# get a reference to the schema in the model. This will get the 1st schema in it.
schema = grt.root.wb.doc.physicalModels[0].catalog.schemata[0]
# iterate through all tables
for table in schema.tables:
    # create a new column object and set its name
    column = grt.classes.db_mysql_Column()
    column.name = "create_time"
    # add it to the table
    table.addColumn(column)
    # set the datatype of the column
    column.setParseType("TIMESTAMP", None)
    column.defaultValue = "CURRENT_TIMESTAMP"

    # same thing for the update_time column
    column = grt.classes.db_mysql_Column()
    column.name = "update_time"
    table.addColumn(column)
    column.setParseType("TIMESTAMP", None)

4 thoughts on “MySQL Workbench: Script for adding columns to all tables in a model”

  1. Hello,
    I had a problem with this script. Data Types were not parsed to tables. I created one more variable and all is OK.

    datatypes = grt.root.wb.rdbmsMgmt.rdbms[0].simpleDatatypes

    I used datatypes instead none in column.setParseType(“DATETIME”, datatypes)

    Petr Slavicek

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.