G
G
gagarinten2021-12-02 20:02:04
Flask
gagarinten, 2021-12-02 20:02:04

Error in flask-sqlalchemy: sqlite3.OperationalError: no such table: users, how to fix?

I have db.py:

from run import db
from werkzeug.security import generate_password_hash, check_password_hash


class User(db.Model):
    __tablename__ = "User"
    id = db.Column(db.Integer, primary_key=True)
    nickname = db.Column(db.String(64), index=True, unique=True)
    password = db.Column(db.String(120), index=True, unique=True)
    email = db.Column(db.String(120), index=True, unique=True)

    def __repr__(self):
        return '<User %r>' % (self.nickname)

    def set_password(self, password):
        self.password_hash = generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(self.password_hash, password)

Reception route:
@app.route('/register', methods=['GET', 'POST'])
def register():
    print("Logging")
    if current_user.is_authenticated:
        print("auth")
        return redirect(url_for('index'))
    form = RegisterForm()
    print("Form")
    if form.is_submitted():
        user = tools.db.User(nickname=form.username.data, email=form.email.data, password=form.password.data)
        user.set_password(form.password.data)
        db.session.add(user)
        db.session.commit()
        flash('Вы зарегистрированы!')
        print("login")
        return redirect(url_for('login'))
    print("Register")
    return render_template('register.html', title='Регистрация', form=form)

And the form itself:
class RegisterForm(FlaskForm):
    username = StringField('Nickname', validators=[DataRequired()])
    email = StringField('Email', validators=[wtforms.validators.Email(), DataRequired()])
    password = PasswordField('Password', validators=[DataRequired()])
    submit = SubmitField("Register")

And when I register (I fill out the forms on / register and press Sumbit), they give me an error:
sqlite3.OperationalError: no such table: users

How to fix it?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question