gpt4 book ai didi

python - Flask - 数据创建

转载 作者:行者123 更新时间:2023-12-01 04:46:09 26 4
gpt4 key购买 nike

我正在学习本教程。

http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-vi-profile-page-and-avatars

我已进入第 6 部分,但我犯了一个错误并删除了数据库。

我回到了第四部分

http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-iv-database

我使用脚本 db_create.py 重新创建了数据库并迁移了它。

但是,我正在尝试添加几行数据。我这样做了

from app import db, models 

上面的行没问题

但是我添加了这一行 u = models.User(nickname='john', email='john@email.com') 突然间,我遇到了错误(请参阅跟踪如下)。

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 2, in __init__
File "/home/andy.kwok/microblog/flask/lib/python2.7/site-packages/sqlalchemy/orm/instrumentation.py", line 324, in _new_state_if_none
state = self._state_constructor(instance, self)
File "/home/andy.kwok/microblog/flask/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py", line 725, in __get__
obj.__dict__[self.__name__] = result = self.fget(obj)
File "/home/andy.kwok/microblog/flask/lib/python2.7/site-packages/sqlalchemy/orm/instrumentation.py", line 158, in _state_constructor
self.dispatch.first_init(self, self.class_)
File "/home/andy.kwok/microblog/flask/lib/python2.7/site-packages/sqlalchemy/event/attr.py", line 260, in __call__
fn(*args, **kw)
File "/home/andy.kwok/microblog/flask/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line 2702, in _event_on_first_init
configure_mappers()
File "/home/andy.kwok/microblog/flask/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line 2598, in configure_mappers
mapper._post_configure_properties()
File "/home/andy.kwok/microblog/flask/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line 1696, in _post_configure_properties
prop.init()
File "/home/andy.kwok/microblog/flask/lib/python2.7/site-packages/sqlalchemy/orm/interfaces.py", line 144, in init
self.do_init()
File "/home/andy.kwok/microblog/flask/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py", line 1550, in do_init
self._process_dependent_arguments()
File "/home/andy.kwok/microblog/flask/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py", line 1606, in _process_dependent_arguments
self.target = self.mapper.mapped_table
File "/home/andy.kwok/microblog/flask/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py", line 725, in __get__
obj.__dict__[self.__name__] = result = self.fget(obj)
File "/home/andy.kwok/microblog/flask/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py", line 1523, in mapper
argument = self.argument()
File "/home/andy.kwok/microblog/flask/lib/python2.7/site-packages/sqlalchemy/ext/declarative/clsregistry.py", line 283, in __call__
(self.prop.parent, self.arg, n.args[0], self.cls)
sqlalchemy.exc.InvalidRequestError: When initializing mapper Mapper|User|user, expression 'Post' failed to locate a name ("name 'Post' is not defined"). If this is a class name, consider adding this relationship() to the <class 'app.models.User'> class after both dependent classes have been defined.

为什么?

救命,我把头发扯下来了!

最佳答案

这可能是因为当加载 User 时,它会尝试查找尚未加载的 Post

您可以尝试在 Post 模型中查找 __tablename__ 。现在,在定义了与 PostrelationshipUser 模型中,使用 Post 中的字符串__表名__。这应该可行。

关于python - Flask - 数据创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29364467/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com