由于我是Python和Flask的新手,我被困住了。目前我正在关注一个名为Learning flask的视频课程。 我创建了一个模型但是当我尝试启动应用程序时出现错误
from flask_sqlalchemy import SQLAlchemy
from werkzeug import generate_password_hash, check_password_hash
db = SQLAlchemy()
class User(db.Model):
__tablename__ = 'users'
uid = db.Column(db.Integer, primary_key =True)
firstname = db.Column(db.string(100))
lastname = db.Column(db.string(100))
email = db.Column(db.String(120), unique=True)
pwdhash = db.Column(db.String(54))
def __init__(self, firstname, lastname, email, password):
self.firstname = firstname.title()
self.lastname = lastname.title()
self.email = email.lower()
self.set_password(password)
def set_password(self, password):
self.pwdhash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.pwdhash, password)
(venv)smurf@tesla:~/WorkInProgress/learning-flask$ python routes.py
Traceback (most recent call last):
File "routes.py", line 2, in <module>
from models import db
File "/home/smurf/WorkInProgress/learning-flask/models.py", line 6, in <module>
class User(db.Model):
File "/home/smurf/WorkInProgress/learning-flask/models.py", line 9, in User
firstname = db.Column(db.string(100))
AttributeError: 'SQLAlchemy' object has no attribute 'string'
答案 0 :(得分:1)
字符串应该大写
firstname = db.Column(db.String(100))
lastname = db.Column(db.String(100))
答案 1 :(得分:0)
db = SQLAlchemy()
需要定义引擎。因此,像db = SQLAlchemy(app)
这样的东西将提供一个模型。您需要定义引擎配置,例如“应用”并将其传递给通话。
也如上所述,您还需要修复语法错误。