Sqlalchemy反射的先决条件是什么?

发布于 2025-01-23 12:06:15 字数 3355 浏览 0 评论 0 原文

我最近将项目和数据库迁移到另一台服务器。这两个服务器都是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}

请注意,上面的相同代码适用于旧数据库。因此,我对如何解决这个问题有些迷失。所以,我想知道我现在该怎么办?

I recently migrated the project and databases to another server. Both servers are ubuntu 20.04.

About the new database(MySql), I created the databse first and then loads the dumped file from the old databse. So basically the databses are the same.

However, when I tried to use sqlalchemy to reflect on the new database, it does not work and throws out errors like this:

> 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'

And the code is below:

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}

Pleas note that, the same code above works for the old database. So, I am kind of lost on how to approach this problem. So, I am wondering what can I do now?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文