【问题标题】:Change column flags in MySQL Workbench scripting shell在 MySQL Workbench 脚本外壳中更改列标志
【发布时间】:2013-03-19 23:36:38
【问题描述】:

为了保证db的一致性,我想批量设置每张表最后一列的类型为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 脚本更改列标志(即:UNSIGNEDZEROFILL)?

【问题讨论】:

    标签: python mysql-workbench


    【解决方案1】:

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

    【讨论】:

    • 如何添加NOT NULL.append("NOT NULL") 添加NOT NULL NULL
    【解决方案2】:

    要添加的是 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
    

    【讨论】:

      【解决方案3】:

      我知道这个问题被问到已经 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)","")
      

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

      【讨论】:

        猜你喜欢
        • 2019-04-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多