我遵循本教程。
http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-vi-profile-page-and-avatars
我在第6部分,但我犯了一个错误并删除了数据库。
我回到第4部分
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.
为什么?
帮助,我脱掉了头发!
答案 0 :(得分:2)
这可能是因为在加载User
时,它正在尝试查找尚未加载的Post
。
您可以尝试在__tablename__
模型中查找Post
。现在,在User
模型中,您已定义relationship
到Post
,请使用Post
的{{1}}中的字符串。这应该有用。