Sqlalchemy反射的先决条件是什么?
我最近将项目和数据库迁移到另一台服务器。这两个服务器都是Ubuntu 20.04。
关于新的数据库(MySQL),我首先创建数据库,然后从旧数据库中加载倾倒文件。因此,基本上数据是相同的。
但是,当我尝试使用sqlalchemy来反思新数据库时,它不起作用并抛出这样的错误:
> File "/Users/max/Dropbox/Python Code/ym/db_api.py", line 6, in
> <module>
> from db import engine as ym_engine File "/Users/max/Dropbox/Python Code/ym/db.py", line 7, in <module>
> Base.metadata.reflect(engine) File "/Users/max/opt/anaconda3/lib/python3.9/site-packages/sqlalchemy/sql/schema.py",
> line 4711, in reflect
> Table(name, self, **reflect_opts) File "<string>", line 2, in __new__ File "/Users/max/opt/anaconda3/lib/python3.9/site-packages/sqlalchemy/util/deprecations.py",
> line 298, in warned
> return fn(*args, **kwargs) File "/Users/max/opt/anaconda3/lib/python3.9/site-packages/sqlalchemy/sql/schema.py",
> line 601, in __new__
> metadata._remove_table(name, schema) File "/Users/max/opt/anaconda3/lib/python3.9/site-packages/sqlalchemy/util/langhelpers.py",
> line 70, in __exit__
> compat.raise_( File "/Users/max/opt/anaconda3/lib/python3.9/site-packages/sqlalchemy/util/compat.py",
> line 207, in raise_
> raise exception File "/Users/max/opt/anaconda3/lib/python3.9/site-packages/sqlalchemy/sql/schema.py",
> line 596, in __new__
> table._init(name, metadata, *args, **kw) File "/Users/max/opt/anaconda3/lib/python3.9/site-packages/sqlalchemy/sql/schema.py",
> line 671, in _init
> self._autoload( File "/Users/max/opt/anaconda3/lib/python3.9/site-packages/sqlalchemy/sql/schema.py",
> line 706, in _autoload
> conn_insp.reflect_table( File "/Users/max/opt/anaconda3/lib/python3.9/site-packages/sqlalchemy/engine/reflection.py",
> line 794, in reflect_table
> self._reflect_fk( File "/Users/max/opt/anaconda3/lib/python3.9/site-packages/sqlalchemy/engine/reflection.py",
> line 947, in _reflect_fk
> fkeys = self.get_foreign_keys( File "/Users/max/opt/anaconda3/lib/python3.9/site-packages/sqlalchemy/engine/reflection.py",
> line 564, in get_foreign_keys
> return self.dialect.get_foreign_keys( File "<string>", line 2, in get_foreign_keys File
> "/Users/max/opt/anaconda3/lib/python3.9/site-packages/sqlalchemy/engine/reflection.py",
> line 55, in cache
> ret = fn(self, con, *args, **kw) File "/Users/max/opt/anaconda3/lib/python3.9/site-packages/sqlalchemy/dialects/mysql/base.py",
> line 3124, in get_foreign_keys
> self._correct_for_mysql_bugs_88718_96365(fkeys, connection) File "/Users/max/opt/anaconda3/lib/python3.9/site-packages/sqlalchemy/dialects/mysql/base.py",
> line 3203, in _correct_for_mysql_bugs_88718_96365
> fkey["referred_table"] = rec["TABLENAME"] KeyError: 'TABLENAME'
代码如下:
from sqlalchemy import MetaData, create_engine
from sqlalchemy.ext.declarative import declarative_base
DB_URL = 'mysql+pymysql://root:pass@server_ip:3306/mh'
engine = create_engine(DB_URL, echo=False)
Base = declarative_base()
Base.metadata.reflect(engine)
class Manga(Base):
__table__ = Base.metadata.tables['cartoon_list']
def as_dict(self):
return {c.name: getattr(self, c.name) for c in self.__table__.columns}
请注意,上面的相同代码适用于旧数据库。因此,我对如何解决这个问题有些迷失。所以,我想知道我现在该怎么办?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论