我想为我的 SQLAlchemy 基类使用类型,但似乎找不到类型定义。
例如:
from sqlalchemy.types import BigInteger
from sqlalchemy.schema import Column, Index
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class User(Base):
__tablename__ = "users"
id_user = Column(
BigInteger, primary_key=True, autoincrement=True, nullable=False, index=True
)
username = Column(String, unique=True, index=True)
现在在引用这个 User 类时,我不知道使用什么类型。此外,如果我想要一个包含 table_name 的字典:模型映射,如:
from typing import Any, Dict
table_model_map: Dict[str, Any] = {
"users": User,
"another_table": AnotherTableModel,
}
我将如何定义此 Dict 的类型
非常感谢您帮助我理解这一点!
最佳答案
Base
是所有模型(也是类型)继承的类型,所以
Dict[str, Base]
关于python - 我对 SQLalchemy declarative_base 使用什么类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58325495/