我正在尝试从一个数据库中的表中提取模式并将其传输到另一个数据库。以下是执行此操作的函数的一部分。Table( ... )
调用时出错。
功能示例:
def transfer_data(self, from_db, to_db, tables, flush_interval=1000, table_data_filters = {}):
if from_db is to_db or from_db == to_db:
raise Exception("""Usage: Can't transfer tables to and from the same database!""")
source, sengine = (from_db.new_session(), from_db.engine()) if isinstance(from_db, SessionManager) else self.__make_session(from_db)
dest, dengine = (to_db.new_session(), to_db.engine()) if isinstance(to_db, SessionManager) else self.__make_session(to_db)
meta = MetaData()
with source.no_autoflush and dest.no_autoflush:
for table_name in tables:
print 'Processing', table_name
print 'Pulling schema from source server'
# ERROR OCCURS BELOW WITH CALL TO Table
table = Table(table_name, meta, autoload=True, autoload_with=sengine)
# etc...
# etc...
错误输出:
table = Table(table_name, meta, autoload=True, autoload_with=sengine)
File "C:\Anaconda\lib\site-packages\sqlalchemy\schema.py", line 332, in __new__
table._init(name, metadata, *args, **kw)
File "C:\Anaconda\lib\site-packages\sqlalchemy\schema.py", line 396, in _init
self._autoload(metadata, autoload_with, include_columns)
File "C:\Anaconda\lib\site-packages\sqlalchemy\schema.py", line 413, in _autoload
self, include_columns, exclude_columns
File "C:\Anaconda\lib\site-packages\sqlalchemy\engine\base.py", line 1595, in run_callable
return conn.run_callable(callable_, *args, **kwargs)
File "C:\Anaconda\lib\site-packages\sqlalchemy\engine\base.py", line 1118, in run_callable
return callable_(self, *args, **kwargs)
File "C:\Anaconda\lib\site-packages\sqlalchemy\engine\default.py", line 262, in reflecttable
return insp.reflecttable(table, include_columns, exclude_columns)
File "C:\Anaconda\lib\site-packages\sqlalchemy\engine\reflection.py", line 397, in reflecttable
for col_d in self.get_columns(table_name, schema, **tblkw):
File "C:\Anaconda\lib\site-packages\sqlalchemy\engine\reflection.py", line 254, in get_columns
**kw)
File "<string>", line 1, in <lambda>
File "C:\Anaconda\lib\site-packages\sqlalchemy\engine\reflection.py", line 49, in cache
ret = fn(self, con, *args, **kw)
File "C:\Anaconda\lib\site-packages\sqlalchemy\dialects\sqlite\base.py", line 779, in get_columns
qtable = quote(table_name)
File "C:\Anaconda\lib\site-packages\sqlalchemy\sql\compiler.py", line 2350, in quote_identifier
self._escape_identifier(value) + \
File "C:\Anaconda\lib\site-packages\sqlalchemy\sql\compiler.py", line 2331, in _escape_identifier
return value.replace(self.escape_quote, self.escape_to_quote)
AttributeError: 'Table' object has no attribute 'replace'
观察:
我认为这与 unicode(即 basestring)以及 sql 编译器如何处理它有关,因为 unicode 具有替换功能。如果 value 的类型比我想象的要取决于数据库模式的编码(在我的情况下是 unicode),则 Table 通过类中的一些动态混合或通过一些其他 oop 机制/设计模式成为一种 unicode 类型,其中 replace 是隐式调用或在某处重新定义的继承属性/操作。
源代码链接:https ://bitbucket.org/shaung/sqlalchemy/src/b0a77b7e42f1/lib/sqlalchemy/sql/compiler.py
文档: http ://docs.sqlalchemy.org/en/rel_0_8/core/schema.html#metadata-reflection
任何帮助将不胜感激。- 我在开发环境中使用 Anaconda 1.6.2 - 我使用 postgres 作为数据库后端,但由于数据库非常小,所以切换到 sqlite。- SQL Alchemy 模型使用声明性语法,因此炼金术使用反射 - Windows 7、64 位(在 Ubuntu 上)