绑定元数据RemovedIn20在调试模式下学习



我使用SQLAlchemy 1.4.0beta1,并为引擎和会话启用了future标志。通常我不会收到警告。但在调试模式下,我会收到2.0风格的select语句的警告。

我的型号.py:

from sqlalchemy.orm import declarative_base
Base = declarative_base()
class Source(Base):
__tablename__ = "server"
id = Column("id", Integer, primary_key=True)
name = Column("server", String(255))
host = Column("host", String(255))
port = Column("port", Integer)
username = Column("login", String(255))
password = Column("password", String(255))
database = Column("db", String(255))

带警告的代码:

from sqlalchemy import select
from sqlalchemy.orm import Session
with Session(engine, future=True) as session:
stmt = select(Source).where(Source.name == source__name)
source = session.execute(stmt).scalar()

警告本身:

/virtualenv/lib/python3.8/site-packages/sqlalchemy/sql/elements.py:489: RemovedIn20Warning: The Executable.bind attribute is considered legacy as of the 1.x series of SQLAlchemy and will be removed in 2.0. Bound metadata is being removed as of SQLAlchemy 2.0. (Background on SQLAlchemy 2.0 at: http://sqlalche.me/e/b8d9)
elif self.bind:

如果我没有在任何地方绑定任何元数据,为什么会有任何警告?当我在调试模式中收到这个警告时,我也无法在提到的文件行到达断点。

完整的示例

import toml
from sqlalchemy import Column, Integer, String, select
from sqlalchemy.engine import create_engine, URL
from sqlalchemy.orm import declarative_base, Session
Base = declarative_base()
with open("config.toml") as stream:
config = toml.load(stream)
config = config["Database"]
engine_url = URL.create(
drivername="mysql+pymysql",
host=config["host"],
port=config["port"],
username=config["username"],
password=config["password"],
database=config["database"],
query={"charset": "utf8"},
)
engine = create_engine(
engine_url,
pool_recycle=3600,
pool_pre_ping=True,
encoding="utf-8",
future=True,
)

class Source(Base):
__tablename__ = "server"
id = Column("id", Integer, primary_key=True)
name = Column("server", String(255))
host = Column("host", String(255))
port = Column("port", Integer)
username = Column("login", String(255))
password = Column("password", String(255))
database = Column("db", String(255))

with Session(engine, future=True) as session:
stmt = select(Source).where(Source.name == "test")
source = session.execute(stmt).scalar()

我把断点放在stmt的第行,并在到达source的第行后显示警告。

PyCharm评估本地对象的所有属性。弃用的bind恰好是其中之一。
>>> stmt.bind
Traceback (most recent call last):
File "/pycharm/plugins/python/helpers/pydev/_pydevd_bundle/pydevd_exec2.py", line 3, in Exec
exec(exp, global_vars, local_vars)
File "<input>", line 1, in <module>
File "<string>", line 2, in bind
File "/virtualenvs/repeater-dIy575E2-py3.8/lib/python3.8/site-packages/sqlalchemy/util/deprecations.py", line 368, in warned
_warn_with_version(message, version, wtype, stacklevel=3)
File "/virtualenvs/repeater-dIy575E2-py3.8/lib/python3.8/site-packages/sqlalchemy/util/deprecations.py", line 41, in _warn_with_version
warnings.warn(warn, stacklevel=stacklevel + 1)
sqlalchemy.exc.RemovedIn20Warning: The Executable.bind attribute is considered legacy as of the 1.x series of SQLAlchemy and will be removed in 2.0. Bound metadata is being removed as of SQLAlchemy 2.0. (Background on SQLAlchemy 2.0 at: http://sqlalche.me/e/b8d9)

最新更新