报错信息:
>>> db
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'db' is not defined
>>> User
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'User' is not defined
出错来源:
《Flask Web开发》5.10 集成Python Shell,P56。
上百度搜到的结果,点开其中几个,从其中的一个,【I think you probably need to run some of the other code first so that you define db and your table schema. Then you can run db.create_all().】受到启发,应该是有某步代码没有执行,导致出现了错误。赶紧往回翻书。翻到5.8.1 创建表(P50),这里有:
(venv) $ python hello.py shell
>>> from hello import db
>>> db.create_all()
明白了,执行了导入db和User的动作之后,这个问题没了。
参考文献:
1.《Flask Web开发》。
2.SQLAlchemy NameError: Name ‘db’ is not defined (?)。