使用Shell Script在mySQL Workbench Model中添加列

时间:2017-05-24 13:37:04

标签: python mysql shell

我需要将以下2列添加到模型中的所有表(mwb文件)

  • created
  • updated

因为有许多表需要修改,而不是手动执行,我找到了following script

# 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 = "created"
    # 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 = "updated"
    table.addColumn(column)
    column.setParseType("TIMESTAMP", None)

脚本工作正常(我将列添加到我的所有表中)但这些列添加在每个表的末尾。

我想将它们添加到第二和第三位置,紧接在第一个位置的主键之后。

我查看了db_mysql_Column定义,但我看不到与该职位相关的任何数据。

此外,addColumn方法不允许使用某种位置重载

有办法吗?

1 个答案:

答案 0 :(得分:0)

我不使用python,但使用此信息: MySQL syntax error using python to add column to a table 你应该能够做到这一点:

img { width: 50%; } @media screen and (max-width: 500px) { img { width: 100%; } } {mytable的{1}} {NEWCOL {1}} {idcol {1}}

使用ALTER TABLE

就个人而言,我会使用sh循环遍历表名列表并将alter stmts写出一个文件然后我将通过mysql客户端执行。我有点懒惰。