0

为了保证数据库的一致性,我想批量设置每个表的最后一列的类型为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. UNSIGNED我还尝试将列的 dataType 属性设置为对具有标志(通过 GUI 定义)的列的 dataType 属性的引用。

最后我尝试了:

column.setParseType('TINYINT(1) UNSIGNED')

但它返回 0 并且不会改变任何东西(如果我删除它会返回 1,UNSIGNED所以我认为它不适用于标志)。

有没有办法在 MySQL Workbench 中使用 Python 脚本更改列标志(即: UNSIGNED、 )?ZEROFILL

4

3 回答 3

3

你需要使用 grt.root.wb.doc.physicalModels[0].catalog.schemata[0].tables[1].columns[7].flags.append('UNSIGNED')

于 2013-04-17T16:51:01.953 回答
0

要添加的是白色 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
于 2016-01-15T05:08:01.710 回答
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)","")

这提供了有关如何执行此操作的基本理解

于 2016-03-03T10:25:52.500 回答