为了确保数据库的一致性,我想将每个表的最后一列的类型批量设置为TINYINT(1) UNSIGNED NOT NULL
。
我发现如何遍历表并定位最后一列,更改其类型并设置NOT NULL
标志,但我无法找到如何设置UNSIGNED
标志。
我试过了两个:
column = grt.root.wb.doc.physicalModels[0].catalog.schemata[0].tables[1].columns[7]
column.flags = ['UNSIGNED']
column.simpleType.flags = ['UNSIGNED']
但我得到TypeError: flag is read-only
。我还尝试将列的dataType属性设置为对具有UNSIGNED
标志(通过GUI定义)的列的dataType属性的引用。
最后我尝试了:
column.setParseType('TINYINT(1) UNSIGNED')
但它返回0并且不会更改任何内容(如果我删除UNSIGNED
则会返回1,所以我认为它不能使用标记)。
有没有办法在MySQL Workbench中使用Python脚本更改列标志(即:UNSIGNED
,ZEROFILL
)?
答案 0 :(得分:3)
您需要使用grt.root.wb.doc.physicalModels[0].catalog.schemata[0].tables[1].columns[7].flags.append('UNSIGNED')
答案 1 :(得分:0)
要添加的是whit column.isNoNull = 1
# -*- coding: utf-8 -*-
# MySQL Workbench Python script
# <description>
# Written in MySQL Workbench 6.3.4
import grt
#import mforms
# 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 = "auditoria_fecha_creacion"
# add it to the table
table.addColumn(column)
# set the datatype of the column
column.setParseType("TIMESTAMP", None)
column.defaultValue = "CURRENT_TIMESTAMP"
column.isNotNull=1
答案 2 :(得分:0)
我知道自提出问题以来已经过了2年
import grt
#import mforms
# tables you want to skip
skip_tables = ["main_defs","some_other_table"]
def addColumn(table,name,datatype,defaultvalue):
# skip this column_name if there is already a column with this name
column_names = [x.name for x in table.columns]
if name in column_names:
return
column = grt.classes.db_mysql_Column()
column.name = name
table.addColumn(column)
column.setParseType(datatype, datatypes)
column.defaultValue = defaultvalue
column.isNotNull=1
# 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]
datatypes = grt.root.wb.rdbmsMgmt.rdbms[0].simpleDatatypes
# iterate through all tables
for table in schema.tables:
# skip the current table if it is in skip_tables
if table.name in skip_tables:
continue
addColumn(table,"created_at","varchar(45)","")
addColumn(table,"updated_at","varchar(45)","")
这提供了有关如何执行此操作的基本知识