flask - 数据创建

flask - data creation

我正在学习本教程。

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.

为什么?

救命啊,我头发都掉了!

这可能是因为在加载 User 时,它正在尝试寻找尚未加载的 Post

您可以尝试在 Post 模型中寻找 __tablename__。现在,在定义了 relationshipPostUser 模型中,使用 Post__tablename__ 中的字符串。那应该有用。