为了账号安全,请及时绑定邮箱和手机立即绑定

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

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 回答

新版本的 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)....

7 回复 有任何疑惑可以回复我~
#1

鹏鹏ff

还是有点看不明白,能详细点吗
2022-02-11 回复 有任何疑惑可以回复我~
#2

慕圣5288765 回复 鹏鹏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 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

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

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信