python - Sqlalchemy - 核心 - 创建现有表的副本

标签 python oracle sqlalchemy

我需要基于同一(Oracle)模式中的现有表创建一个表。我不想对新表有任何约束,即使原始表可能有一个或多个。

我尝试使用column.copy()从原始表创建新表,但由于某种原因,我无法在数据库中创建新表后删除约束。

def clone_table_approach_1(original_table, connection, metadata):
    try:
        new_table_name = original_table.name + '_sync'
        columns = [c.copy() for c in original_table.columns]
        new_table = Table(new_table_name, metadata, quote=False, *columns)

        # Create table in database
        if not new_table.exists():
            new_table.create()
        else:
            raise Exception("New table already exists")

        # Remove constraints from new table if any
        for constraint in new_table.constraints:
            connection.execute(DropConstraint(constraint))

        # Return table handle for newly created table
        final_cloned_table = Table(new_table, metadata, quote=False)
        return final_cloned_table

    except:
        # Drop if we did create a new table
        if new_table.exists():
            new_table.drop()
        raise

删除约束时会失败。 sqlalchemy 似乎不知道在数据库中的新表上创建的约束的名称。我可以看到新表的约束名称如“SYS_C00450822”和“SYS_C00450823”(这些不是 NULL 检查约束)。

异常(exception):

Traceback (most recent call last):
  File "/home/gaurav/myprojects/python/sync/test_table_copy.py", line 163, in <module>
    t_product_new = clone_table_approach_1(t_product, target_conn, target_metadata)
  File "/home/gaurav/myprojects/python/sync/test_table_copy.py", line 57, in clone_table_approach_1
    connection.execute(DropConstraint(constraint))
  File "/home/gaurav/.local/lib/python3.4/site-packages/sqlalchemy/engine/base.py", line 729, in execute
    return meth(self, multiparams, params)
  File "/home/gaurav/.local/lib/python3.4/site-packages/sqlalchemy/sql/ddl.py", line 69, in _execute_on_connection
    return connection._execute_ddl(self, multiparams, params)
  File "/home/gaurav/.local/lib/python3.4/site-packages/sqlalchemy/engine/base.py", line 777, in _execute_ddl
    compiled = ddl.compile(dialect=dialect)
  File "<string>", line 1, in <lambda>
  File "/home/gaurav/.local/lib/python3.4/site-packages/sqlalchemy/sql/elements.py", line 493, in compile
    return self._compiler(dialect, bind=bind, **kw)
  File "/home/gaurav/.local/lib/python3.4/site-packages/sqlalchemy/sql/ddl.py", line 27, in _compiler
    return dialect.ddl_compiler(dialect, self, **kw)
  File "/home/gaurav/.local/lib/python3.4/site-packages/sqlalchemy/sql/compiler.py", line 199, in __init__
    self.string = self.process(self.statement, **compile_kwargs)
  File "/home/gaurav/.local/lib/python3.4/site-packages/sqlalchemy/sql/compiler.py", line 222, in process
    return obj._compiler_dispatch(self, **kwargs)
  File "/home/gaurav/.local/lib/python3.4/site-packages/sqlalchemy/sql/visitors.py", line 80, in _compiler_dispatch
    return meth(self, **kw)
  File "/home/gaurav/.local/lib/python3.4/site-packages/sqlalchemy/sql/compiler.py", line 2518, in visit_drop_constraint
    self.preparer.format_constraint(drop.element),
  File "<string>", line 1, in <lambda>
  File "/home/gaurav/.local/lib/python3.4/site-packages/sqlalchemy/sql/compiler.py", line 2928, in format_constraint
    return self.quote(constraint.name)
  File "/home/gaurav/.local/lib/python3.4/site-packages/sqlalchemy/sql/compiler.py", line 2893, in quote
    if self._requires_quotes(ident):
  File "/home/gaurav/.local/lib/python3.4/site-packages/sqlalchemy/sql/compiler.py", line 2864, in _requires_quotes
    lc_value = value.lower()
AttributeError: 'NoneType' object has no attribute 'lower'

还有其他更好的方法吗?

最佳答案

看来我已经解决了我自己的问题。

原始表具有三个“NOT NULL”约束(未显式命名,因此它们被命名为“SYS_C00450822”等)和一个 PK 约束(例如,命名为“TABLE_PK”)。

当我仅使用原始表中的列(但不包含约束)创建副本时,创建的新表具有 4 个约束,所有约束均具有系统生成的名称,例如“SYS_C00450822”。

在删除约束时,sqlalchemy 没有获得约束的任何“名称”,正如问题中提到的,并且在某些地方导致了行 lc_value = value.lower() 因为 NoneType 的值(我认为是“名称”)为 null。

因此,我更改了代码以从原始表创建带有约束的新表 - 但重命名约束的名称,以便它不会与现有的原始表约束冲突。

这以相同的方式创建了三个“NOT NULL”CHECK 约束,但 PK 约束现在用名称定义,例如“TABLE_PK_2”。

然后通过 DropConstraint 调用安全地完成此操作。

我知道我不必仅仅为了创建一个具有原始表定义的新表而执行所有这些操作,但就目前而言,这似乎可行。

关于python - Sqlalchemy - 核心 - 创建现有表的副本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28731288/

相关文章:

python - 使用 Flask/heroku session 总是空的

sql - 如何在 SQL (Oracle) 中随机排列数据?

sql - 在 Oracle 上与 INSERT INTO 结合使用时,有效的 GROUP BY 查询不起作用

python - 动态连接sqlalchemy

python - 如何通过列名从 SQLAlchemy 实例中获取值?

python - 如何更改 sqlalchemy 中预先存在的数据库表的两个不同列标题?

python - 如何通过react.js发出服务器端数据请求

python - 如何将二维数组传递到 pycuda 中的内核?

python - 列表中 x 元素的最后 3 个元素的总和作为新列表 [python 3]

oracle - 列列表中select的join方法