请问 sqlalchemy.exc.InvalidRequestError 如何解决?

sqlalchemy.exc.InvalidRequestError

sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize - can’t proceed with initialization of other mappers. Triggering mapper: ‘mapped class Category->category’. Original exception was: relationship ‘articles’ expects a class or a mapper argument (received: <class ‘sqlalchemy.sql.schema.Table’>)

谢谢各位了!

补充

(jimufun) bash-3.2$ flask forge
Generating the administrator…
Traceback (most recent call last):
File “/Users/matt/.local/share/virtualenvs/jimufun-mIcFJAhi/bin/flask”, line 10, in
sys.exit(main())
File “/Users/matt/.local/share/virtualenvs/jimufun-mIcFJAhi/lib/python3.7/site-packages/flask/cli.py”, line 906, in main
cli.main(args=args, prog_name=name)
File “/Users/matt/.local/share/virtualenvs/jimufun-mIcFJAhi/lib/python3.7/site-packages/flask/cli.py”, line 569, in main
return super(FlaskGroup, self).main(*args, **kwargs)
File “/Users/matt/.local/share/virtualenvs/jimufun-mIcFJAhi/lib/python3.7/site-packages/click/core.py”, line 717, in main
rv = self.invoke(ctx)
File “/Users/matt/.local/share/virtualenvs/jimufun-mIcFJAhi/lib/python3.7/site-packages/click/core.py”, line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File “/Users/matt/.local/share/virtualenvs/jimufun-mIcFJAhi/lib/python3.7/site-packages/click/core.py”, line 956, in invoke
return ctx.invoke(self.callback, **ctx.params)
File “/Users/matt/.local/share/virtualenvs/jimufun-mIcFJAhi/lib/python3.7/site-packages/click/core.py”, line 555, in invoke
return callback(*args, **kwargs)
File “/Users/matt/.local/share/virtualenvs/jimufun-mIcFJAhi/lib/python3.7/site-packages/click/decorators.py”, line 17, in new_func
return f(get_current_context(), *args, **kwargs)
File “/Users/matt/.local/share/virtualenvs/jimufun-mIcFJAhi/lib/python3.7/site-packages/flask/cli.py”, line 419, in decorator
return __ctx.invoke(f, *args, **kwargs)
File “/Users/matt/.local/share/virtualenvs/jimufun-mIcFJAhi/lib/python3.7/site-packages/click/core.py”, line 555, in invoke
return callback(*args, **kwargs)
File “/Users/matt/jimufun/jimufun/init.py”, line 124, in forge
fake_admin()
File “/Users/matt/jimufun/jimufun/fakes.py”, line 15, in fake_admin
community_subtitle=“No, I’m the real thing.”
File “”, line 2, in init
File “/Users/matt/.local/share/virtualenvs/jimufun-mIcFJAhi/lib/python3.7/site-packages/sqlalchemy/orm/instrumentation.py”, line 373, in _new_state_if_none
state = self._state_constructor(instance, self)
File “/Users/matt/.local/share/virtualenvs/jimufun-mIcFJAhi/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py”, line 855, in get
obj.dict[self.name] = result = self.fget(obj)
File “/Users/matt/.local/share/virtualenvs/jimufun-mIcFJAhi/lib/python3.7/site-packages/sqlalchemy/orm/instrumentation.py”, line 199, in state_constructor
self.dispatch.first_init(self, self.class
)
File “/Users/matt/.local/share/virtualenvs/jimufun-mIcFJAhi/lib/python3.7/site-packages/sqlalchemy/event/attr.py”, line 297, in call
fn(*args, **kw)
File “/Users/matt/.local/share/virtualenvs/jimufun-mIcFJAhi/lib/python3.7/site-packages/sqlalchemy/orm/mapper.py”, line 3341, in _event_on_first_init
configure_mappers()
File “/Users/matt/.local/share/virtualenvs/jimufun-mIcFJAhi/lib/python3.7/site-packages/sqlalchemy/orm/mapper.py”, line 3229, in configure_mappers
mapper._post_configure_properties()
File “/Users/matt/.local/share/virtualenvs/jimufun-mIcFJAhi/lib/python3.7/site-packages/sqlalchemy/orm/mapper.py”, line 1947, in _post_configure_properties
prop.init()
File “/Users/matt/.local/share/virtualenvs/jimufun-mIcFJAhi/lib/python3.7/site-packages/sqlalchemy/orm/interfaces.py”, line 196, in init
self.do_init()
File “/Users/matt/.local/share/virtualenvs/jimufun-mIcFJAhi/lib/python3.7/site-packages/sqlalchemy/orm/relationships.py”, line 1860, in do_init
self._process_dependent_arguments()
File “/Users/matt/.local/share/virtualenvs/jimufun-mIcFJAhi/lib/python3.7/site-packages/sqlalchemy/orm/relationships.py”, line 1922, in _process_dependent_arguments
self.target = self.entity.persist_selectable
File “/Users/matt/.local/share/virtualenvs/jimufun-mIcFJAhi/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py”, line 855, in get
obj.dict[self.name] = result = self.fget(obj)
File “/Users/matt/.local/share/virtualenvs/jimufun-mIcFJAhi/lib/python3.7/site-packages/sqlalchemy/orm/relationships.py”, line 1845, in entity
% (self.key, type(argument))
sqlalchemy.exc.ArgumentError: relationship ‘articles’ expects a class or a mapper argument (received: <class ‘sqlalchemy.sql.schema.Table’>)
(jimufun) bash-3.2$

纯代码文本或命令行输出建议使用代码块格式化。

插入代码块的方式:除了将所有代码缩进四格,更方便的插入代码块方式是使用三个连续的反引号作为开始和结束标记,比如:

```
def hello():
    return ‘Hello World’
```

会被渲染为:

 def hello():
     return 'Hello World'

具体请看置顶帖子 技术提问帖发帖规则(创建提问帖前必读)

然后你这个问题,很明显是数据模型使用relationship错误。

检查你的articles模型中relationship关联的Category,正确格式应该是

db.relationship('Category')

注意Category开头大小写(应该是和Category模型名称一样,不是和Category.__tablename__一样,如果不对就反过来)。

谢谢!问题已经得到解决!