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

时间:2015-02-25 22:57:19

标签: 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”(这些是NOT NULL检查约束)。

例外:

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'

还有其他更好的方法吗?

1 个答案:

答案 0 :(得分:2)

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

原始表有三个&#34; NOT NULL&#34;约束(未明确命名,因此它们被命名为&#34; SYS_C00450822&#34;等),以及一个PK约束(命名为&#34; TABLE_PK&#34;)。

当我使用原始表中的列创建副本时,但不是约束,新表创建了4个约束,所有这些都使用系统生成的名称,如&#34; SYS_C00450822&#34;。

在放弃限制的同时,sqlalchemy没有得到任何名字&#34;对于约束,如问题中提到的,以及某个地方,在行lc_value = value.lower()中引起问题,因为值(我认为&#34;名称&#34;,为null,为NoneType)

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

这创造了三个&#34; NOT NULL&#34;以相同的方式检查约束,但PK约束现在定义为名称,比如,&#34; TABLE_PK_2&#34;。

然后安全地通过DropConstraint电话。

我知道我不应该只是为了创建一个包含原始表定义的新表来做所有这些,但是现在,这似乎有效。