我需要将以下两列添加到模型(mwb 文件)中的所有表中
已创建
已更新
因为有很多表需要修改,所以我没有手动修改,而是找到了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方法不允许重载某种位置
有办法吗?
最佳答案
我不使用 python,但使用以下信息: MySQL syntax error using python to add column to a table 你应该能够做到这一点:
ALTER TABLE
mytableADD
newcolINT NOT NULL AFTER
idcol;
使用cursor.execute(query)
就我个人而言,我会使用 sh 循环遍历表名列表并将 alter stmts 写入一个文件,然后通过 mysql 客户端执行该文件。我就是有点懒。
关于python - 使用 Shell 脚本在 mySQL Workbench 模型中添加列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44160033/