search

Home  >  Q&A  >  body text

python - flask copy source code Chapter 8 user role error occurs

I finished Chapter 8 in the afternoon. I can log in to the web page and store data in the database, but I can’t send email messages. So I copied the
source code. Now a new problem has arisen.
Me and the source code The difference is in the database configuration. I directly hard-coded the address, which can be used in the afternoon. I also hard-coded my email address.
It has been tested before and there was no problem. Before, there was no foreign key associated with User and Role. According to the book There is talk of database migration, and I have tried it, but I am not sure. I think there may be a problem with the database. I hope someone can help me. Thank you in advance

Error
sqlalchemy.exc.ProgrammingError
sqlalchemy.exc.ProgrammingError: (pymysql.err.ProgrammingError) (1146, "Table 'flask.users' doesn't exist") [SQL: 'SELECT users .id AS users_id, users.email AS users_email, users.username AS users_username, users.role_id AS users_role_id, users.password_hash AS users_password_hash, users.confirmed AS users_confirmed nFROM users nWHERE users.id = %(param_1)s'] [parameters : {'param_1': 1}]

#coding:utf-8

import os
basedir = os.path.abspath(os.path.dirname(__file__))

class Config:
    SECRET_KEY = 'hard to guess string'
    SSL_DISABLE = False
    SQLALCHEMY_COMMIT_ON_TEARDOWN = True
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    SQLALCHEMY_RECORD_QUERIES = True
    MAIL_SERVER = 'smtp.163.com'
    MAIL_PORT = 25
    MAIL_USE_TLS = True
    MAIL_USERNAME = ('13166337919@163.com')
    MAIL_PASSWORD = ('a123123')
    FLASKY_MAIL_SUBJECT_PREFIX = '[Flasky]'
    FLASKY_MAIL_SENDER = 'Flasky Admin <13166337919@163.com>'
    FLASKY_ADMIN = ('626825701@qq.com')



    @staticmethod
    def init_app(app):
        pass


class DevelopmentConfig(Config):
    DEBUG = True
    SQLALCHEMY_DATABASE_URI ='mysql+pymysql://root:123456@127.0.0.1/flask'



class TestingConfig(Config):
    TESTING = True
    SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:123456@127.0.0.1/text'


class ProductionConfig(Config):
    SQLALCHEMY_DATABASE_URI ='mysql+pymysql://root:123456@127.0.0.1/pro'


config = {
    'development': DevelopmentConfig,
    'testing': TestingConfig,
    'production': ProductionConfig,


    'default': DevelopmentConfig
}
from werkzeug.security import generate_password_hash, check_password_hash
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from flask import current_app
from flask_login import UserMixin
from . import db, login_manager


class Role(db.Model):
    __tablename__ = 'roles'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True)
    users = db.relationship('User', backref='role', lazy='dynamic')

    def __repr__(self):
        return '<Role %r>' % self.name


class User(UserMixin, db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(64), unique=True, index=True)
    username = db.Column(db.String(64), unique=True, index=True)
    role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))
    password_hash = db.Column(db.String(128))
    confirmed = db.Column(db.Boolean, default=False)

    @property
    def password(self):
        raise AttributeError('password is not a readable attribute')

    @password.setter
    def password(self, password):
        self.password_hash = generate_password_hash(password)

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

    def generate_confirmation_token(self, expiration=3600):
        s = Serializer(current_app.config['SECRET_KEY'], expiration)
        return s.dumps({'confirm': self.id})

    def confirm(self, token):
        s = Serializer(current_app.config['SECRET_KEY'])
        try:
            data = s.loads(token)
        except:
            return False
        if data.get('confirm') != self.id:
            return False
        self.confirmed = True
        db.session.add(self)
        return True

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


@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))
世界只因有你世界只因有你2772 days ago817

reply all(1)I'll reply

  • 阿神

    阿神2017-05-18 10:49:39

    The error is very clear
    (pymysql.err.ProgrammingError) (1146, "Table 'flask.users' doesn't exist")

    reply
    0
  • Cancelreply