在MySQL Workbench scripting sh中更改列标志

2024-06-16 12:39:34 发布

您现在位置:Python中文网/ 问答频道 /正文

为了确保数据库的一致性,我想批量设置每个表最后一列的类型为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属性的引用。在

最后我试着:

^{pr2}$

但它返回0,并且不更改任何内容(如果我删除UNSIGNED则返回1,因此我认为它不适用于标志)。在

有没有方法可以在MySQL Workbench中使用Python脚本更改列标志(即:UNSIGNEDZEROFILL)?在


Tags: 数据库类型目标属性标志notcolumn批量
3条回答

加起来是白的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

我知道有人问这个问题已经两年了

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)","")

这提供了一个基本的理解如何做到这一点

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

相关问题 更多 >