__mapper_args__ = {"order_by": country_code}报错unexpected keyword argument 'order_by',是什么原因呢

来源:7-2 SQLAlchemy 开发 COVID-19 模型类

慕慕9595314

2021-04-07 18:10

Traceback (most recent call last):

  File "D:\Anaconda3\envs\fastapi\lib\runpy.py", line 194, in _run_module_as_main

    return _run_code(code, main_globals, None,

  File "D:\Anaconda3\envs\fastapi\lib\runpy.py", line 87, in _run_code

    exec(code, run_globals)

  File "c:\Users\withr\.vscode\extensions\ms-python.python-2021.3.680753044\pythonFiles\lib\python\debugpy\__main__.py", line 45, in <module>

    cli.main()

  File "c:\Users\withr\.vscode\extensions\ms-python.python-2021.3.680753044\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 444, in main

    run()

  File "c:\Users\withr\.vscode\extensions\ms-python.python-2021.3.680753044\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 285, in run_file

    runpy.run_path(target_as_str, run_name=compat.force_str("__main__"))

  File "D:\Anaconda3\envs\fastapi\lib\runpy.py", line 265, in run_path

    return _run_module_code(code, init_globals, run_name,

  File "D:\Anaconda3\envs\fastapi\lib\runpy.py", line 97, in _run_module_code

    _run_code(code, mod_globals, init_globals,

  File "D:\Anaconda3\envs\fastapi\lib\runpy.py", line 87, in _run_code

    exec(code, run_globals)

  File "run.py", line 12, in <module>

    from coronavirus import applications

  File "E:\SyncWorkspace\Code\PY\fastapi\fastapi_covid19\coronavirus\__init__.py", line 8, in <module>     

    from .main import applications

  File "E:\SyncWorkspace\Code\PY\fastapi\fastapi_covid19\coronavirus\main.py", line 13, in <module>        

    from coronavirus import crud, schemas

  File "E:\SyncWorkspace\Code\PY\fastapi\fastapi_covid19\coronavirus\crud.py", line 9, in <module>

    from coronavirus import models, schemas

  File "E:\SyncWorkspace\Code\PY\fastapi\fastapi_covid19\coronavirus\models.py", line 16, in <module>      

    class City(Base):

  File "D:\Anaconda3\envs\fastapi\lib\site-packages\sqlalchemy\orm\decl_api.py", line 75, in __init__      

    _as_declarative(reg, cls, dict_)

  File "D:\Anaconda3\envs\fastapi\lib\site-packages\sqlalchemy\orm\decl_base.py", line 126, in _as_declarative

    return _MapperConfig.setup_mapping(registry, cls, dict_, None, {})

  File "D:\Anaconda3\envs\fastapi\lib\site-packages\sqlalchemy\orm\decl_base.py", line 177, in setup_mapping

    return cfg_cls(registry, cls_, dict_, table, mapper_kw)

  File "D:\Anaconda3\envs\fastapi\lib\site-packages\sqlalchemy\orm\decl_base.py", line 314, in __init__    

    self._early_mapping(mapper_kw)

  File "D:\Anaconda3\envs\fastapi\lib\site-packages\sqlalchemy\orm\decl_base.py", line 200, in _early_mapping

    self.map(mapper_kw)

  File "D:\Anaconda3\envs\fastapi\lib\site-packages\sqlalchemy\orm\decl_base.py", line 971, in map

    mapper_cls(self.cls, self.local_table, **self.mapper_args),

TypeError: __init__() got an unexpected keyword argument 'order_by'

写回答 关注

1回答

  • duskash
    2021-10-04 00:10:49

    新版本的 sqlalchemy 丢弃了 __mappter_args__ 当中设置的方法

    应当用 db.query().order_by() 直接在 Query 对象后面显示地调用 order_by 函数


    这节课里, 有一张 City 表 和一张 Data 表, 对应的 crud 方法里面

    db.query(models.City).order_by(models.City.province).offset().limit().all()

    db.query(models.Data).order_by(models.Data.confirmed)....

    慕圣5288... 回复鹏鹏ff

    def get_cities(db: Session, skip: int = 1, limit: int = 10): # return db.query(models.City).offset(skip).limit(limit).all() return db.query(models.City).order_by(models.City.country_code).offset(skip).limit(limit).all()

    2022-07-06 14:58:36

    共 2 条回复 >

高性能 FastAPI 框架入门精讲

FastAPI 框架快速上手开发,结合 API 交互文档逐个讲解核心模块的使用。

12445 学习 · 87 问题

查看课程

相似问题