[英]sqlalchemy no such table: user
I've setup a flask site and I'm trying to get a signup page to work.我已经设置了一个 Flask 站点,并且正在尝试使注册页面正常工作。 The page itself renders but when I enter information in the form and submit I get a
sqlalchemy.exc.OperationalError
.页面本身呈现,但是当我在表单中输入信息并提交时,我得到一个
sqlalchemy.exc.OperationalError
。 Any help here would be greatly appreciated!!任何帮助在这里将不胜感激!
Here is my signup function:这是我的注册功能:
@auth.route('/signup', methods=['POST'])
def signup_post():
email = request.form.get('email')
name = request.form.get('name')
password = request.form.get('password')
user = User.query.filter_by(email=email).first() # if this returns a user,
# then the email already exists in database
if user: # if a user is found, we want to redirect back to signup page so user can try again
flash('email address already exists')
return redirect(url_for('auth.signup'))
# create a new user with the form data. Hash the password so the plaintext version isn't saved.
new_user = user(email=email, name=name, password=generate_password_hash(password, method='sha256'))
# add the new user to the database
db.session.add(new_user)
db.session.commit()
return redirect(url_for('auth.login'))
The specific error says no such table: user
then:具体错误说
no such table: user
然后:
[SQL: SELECT user.id AS user_id, user.email AS user_email, user.password AS user_password, user.name AS user_name
FROM user
WHERE user.email = ?
LIMIT ? OFFSET ?]
[parameters: ('', 1, 0)]
(Background on this error at: http://sqlalche.me/e/13/e3q8)
Here is where I initialize the DB and create the app:这是我初始化数据库并创建应用程序的地方:
db = SQLAlchemy()
def create_app():
app = Flask(__name__)
app.config['SECRET_KEY'] = 'UMGC-SDEV300-Key'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
db.init_app(app)
login_manager = LoginManager()
login_manager.login_view = 'auth.login'
login_manager.init_app(app)
from .models import User
@login_manager.user_loader
def load_user(user_id):
# since the user_id is just the primary key of our user table, use it in the query for the user
return User.query.get(int(user_id))
# blueprint for auth routes in our app
from .auth import auth as auth_blueprint
app.register_blueprint(auth_blueprint)
# blueprint for non-auth parts of app
from .main import main as main_blueprint
app.register_blueprint(main_blueprint)
return app
Check the models.py and add the line: tablename = 'user'检查models.py并添加行: tablename = 'user'
class User(UserMixin, db.Model):
__tablename__ = 'user'
id = db.Column(db.Integer, primary_key=True) # primary keys are required by SQLAlchemy
firstname = db.Column(db.String(30))
lastname = db.Column(db.String(30))
email = db.Column(db.String(100), uni
And in the init .py change to并在init .py 更改为
@login_manager.user_loader
def load_user(user_id):
return User.query.filter_by(id=user_id).first()
And make sure that file exits,and have the table: user并确保文件退出,并有表:用户
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
I figured it out.我想到了。 I just needed to create the database.
我只需要创建数据库。 I went through this step once when I first created the application but I guess I missed something.
当我第一次创建应用程序时,我曾经历过这一步,但我想我错过了一些东西。 I deleted the database and created a new one using the below command and that did the trick.
我删除了数据库并使用以下命令创建了一个新的数据库,并成功了。
from project import db, create_app
db.create_all(app=create_app())
This was done from the Python REPL.这是从 Python REPL 完成的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.