sqlalchemy 内部错误,由于相同的代码,这看起来不像我的编码问题

问题描述 投票:0回答:3

我只是做了与教程相同的事情,他的作品,但我的失败了。它看起来像终端中显示的 sqlalchemy 内部错误。

我不知道我的代码出了什么问题,我之前所做的是在pycharm社区设置中的项目解释器中安装flask-sqlalthemy、pymysql和flask。

我在这个项目中使用了virtrualenev。

以下代码

编码:utf8

from flask import Flask

from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
import pymysql

app = Flask(__name__)

app.config["SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://root:[email protected]:8889/movie"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True

db = SQLAlchemy(app)


# user
class User(db.Model):
    __tablename__ = "user"
    id = db.Column(db.Integer, primary_key=True)  # id
    name = db.Column(db.String(100), unique=True)  # name
    pwd = db.Column(db.String(100))  # password
    email = db.Column(db.String(100), unique=True)  # email
    phone = db.Column(db.String(11), unique=True)  # phone
    info = db.Column(db.Text, unique=True)  # resume
    face = db.Column(db.String(255), unique=True)  # icon
    addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)  # adding time
    uuid = db.Column(db.String(255), unique=True)  # unique id
    userlogs = db.relationship('Userlog', backref='user')  # user log foreign key relationship
    comments = db.relationship('Comment', backref='user')  # comment foreign key relationship
    moviecols = db.relationship('Moviecol', backref='user')  # movie collection foreign key relationship

    def __repr__(self):
        return "<User %r>" % self.name


# user log
class Userlog(db.Model):
    __tablename__ = "userlog"
    id = db.Column(db.Integer, primary_key=True)  # id
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))  # user id
    ip = db.Column(db.String(100))  # login ip
    addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)  # adding time
    movies = db.relationship('Movie', backref='tag')  # movie tag foreign key relationship

    def __repr__(self):
        return "<Userlog %r>" % self.id


# Tag
class Tag(db.Model):
    __tablename__ = "tag"
    id = db.Column(db.Integer, primary_key=True)  # id
    name = db.Column(db.String(100), unique=True)  # title
    addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)  # adding time

    def __repr__(self):
        return "<Tag %r>" % self.name


# movie
class Movie(db.Model):
    __tablename__ = "movie"
    id = db.Column(db.Integer, primary_key=True)  # id
    title = db.Column(db.String(255), unique=True)  # title
    url = db.Column(db.String(255), unique=True)  # url
    info = db.Column(db.Text, unique=True)  # resume
    logo = db.Column(db.String(255), unique=True)  # logo
    star = db.Column(db.SmallInteger)  # star
    playnum = db.Column(db.BigInteger)  # play number
    commentnum = db.Column(db.BigInteger)  # comment number
    tag_id = db.Column(db.Integer, db.ForeignKey('tag.id'))  # tag id
    area = db.Column(db.String(255))  # area
    release_time = db.Column(db.Date)  # release time
    length = db.Column(db.String(255))  # length
    addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)  # adding time
    comments = db.relationship('Comment', backref='movie')  # comment foreign key relationship
    moviecols = db.relationship('Moviecol', backref='movie')  # movie collection foreign key relationship

    def __repr__(self):
        return "<Movie %r>" % self.title


# Preview
class Preview(db.Model):
    __tablename__ = "preview"
    id = db.Column(db.Integer, primary_key=True)  # id
    title = db.Column(db.String(255), unique=True)  # title
    logo = db.Column(db.String(255), unique=True)  # logo
    addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)  # adding time

    def __repr__(self):
        return "<Preview %r>" % self.title


# Comment
class Comment(db.Model):
    __tablename__ = "comment"
    id = db.Column(db.Integer, primary_key=True)  # id
    content = db.Column(db.Text)  # content
    logo = db.Column(db.String(255), unique=True)  # logo
    addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)  # adding time
    movie_id = db.Column(db.Integer, db.ForeignKey('movie.id'))  # movie id
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))  # movie id
    addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)  # adding time

    def __repr__(self):
        return "<Comment %r>" % self.id


# movie collection
class Moviecol(db.Model):
    __tablename__ = "moviecol"
    id = db.Column(db.Integer, primary_key=True)  # id
    movie_id = db.Column(db.Integer, db.ForeignKey('movie.id'))  # movie id
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))  # movie id
    addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)  # adding time

    def __repr__(self):
        return "<Moviecol %r>" % self.id


# Authority
class Auth(db.Model):
    __tablename__ = "auth"
    id = db.Column(db.Integer, primary_key=True)  # id
    name = db.Column(db.String(100), unique=True)  # title
    url = db.Column(db.String(255), unique=True)  # movie id
    addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)  # adding time

    def __repr__(self):
        return "<Auth %r>" % self.name


# Role
class Role(db.Model):
    __tablename__ = "role"
    id = db.Column(db.Integer, primary_key=True)  # id
    name = db.Column(db.String(100), unique=True)  # title
    auths = db.Column(db.String(600))  # movie id
    addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)  # adding time

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


# Admin
class Admin(db.Model):
    __tablename__ = "admin"
    id = db.Column(db.Integer, primary_key=True)  # id
    name = db.Column(db.String(100), unique=True)  # name
    pwd = db.Column(db.String(100))  # password
    is_super = db.Column(db.SmallInteger)  # whether is super admin, 0 is super admin
    role_id = db.Column(db.Integer, db.ForeignKey('role.id'))  # role id
    addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)  # adding time
    adminlogs = db.relationship('Adminlog', backref='admin')  # admin login log foreign key relationship
    oplogs = db.relationship('Oplog', backref='admin')  # admin operation log foreign key relationship

    def __repr__(self):
        return "<Admin %r>" % self.name


# Admin login log
class Adminlog(db.Model):
    __tablename__ = "adminlog"
    id = db.Column(db.Integer, primary_key=True)  # id
    admin_id = db.Column(db.Integer, db.ForeignKey('admin.id'))  # admin id
    ip = db.Column(db.String(100))  # login ip
    addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)  # adding time

    def __repr__(self):
        return "<Adminlog %r>" % self.id


# Admin operation log
class Oplog(db.Model):
    __tablename__ = "oplog"
    id = db.Column(db.Integer, primary_key=True)  # id
    admin_id = db.Column(db.Integer, db.ForeignKey('admin.id'))  # user id
    ip = db.Column(db.String(100))  # login ip
    reason = db.Column(db.String(600))  # reason
    addtime = db.Column(db.DateTime, index=True, default=datetime.utcnow)  # adding time

    def __repr__(self):
        return "<Oplog %r>" % self.id


if __name__ == "__main__":
    db.create_all()
#
# the error below
    (venv) D:\Python\movie_project\app>python models.py

    # the error below


   Traceback (most recent call last):
  File "models.py", line 188, in <module>
    db.create_all()
  File "D:\Python\movie_project\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 1033, in create_all
    self._execute_for_all_tables(app, bind, 'create_all')
  File "D:\Python\movie_project\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 1025, in _execute_for_all_tables
    op(bind=self.get_engine(app, bind), **extra)
  File "D:\Python\movie_project\venv\lib\site-packages\sqlalchemy\sql\schema.py", line 4287, in create_all
    ddl.SchemaGenerator, self, checkfirst=checkfirst, tables=tables
  File "D:\Python\movie_project\venv\lib\site-packages\sqlalchemy\engine\base.py", line 2032, in _run_visitor
    with self._optional_conn_ctx_manager(connection) as conn:
  File "E:\Program Files\Python\lib\contextlib.py", line 112, in __enter__
    return next(self.gen)
  File "D:\Python\movie_project\venv\lib\site-packages\sqlalchemy\engine\base.py", line 2024, in _optional_conn_ctx_manager
    with self._contextual_connect() as conn:
  File "D:\Python\movie_project\venv\lib\site-packages\sqlalchemy\engine\base.py", line 2226, in _contextual_connect
    self._wrap_pool_connect(self.pool.connect, None),
  File "D:\Python\movie_project\venv\lib\site-packages\sqlalchemy\engine\base.py", line 2262, in _wrap_pool_connect
    return fn()
  File "D:\Python\movie_project\venv\lib\site-packages\sqlalchemy\pool\base.py", line 363, in connect
    return _ConnectionFairy._checkout(self)
  File "D:\Python\movie_project\venv\lib\site-packages\sqlalchemy\pool\base.py", line 760, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "D:\Python\movie_project\venv\lib\site-packages\sqlalchemy\pool\base.py", line 492, in checkout
    rec = pool._do_get()
  File "D:\Python\movie_project\venv\lib\site-packages\sqlalchemy\pool\impl.py", line 139, in _do_get
    self._dec_overflow()
  File "D:\Python\movie_project\venv\lib\site-packages\sqlalchemy\util\langhelpers.py", line 68, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "D:\Python\movie_project\venv\lib\site-packages\sqlalchemy\util\compat.py", line 129, in reraise
    raise value
  File "D:\Python\movie_project\venv\lib\site-packages\sqlalchemy\pool\impl.py", line 136, in _do_get
    return self._create_connection()
  File "D:\Python\movie_project\venv\lib\site-packages\sqlalchemy\pool\base.py", line 308, in _create_connection
    return _ConnectionRecord(self)
  File "D:\Python\movie_project\venv\lib\site-packages\sqlalchemy\pool\base.py", line 437, in __init__
    self.__connect(first_connect_check=True)
  File "D:\Python\movie_project\venv\lib\site-packages\sqlalchemy\pool\base.py", line 639, in __connect
    connection = pool._invoke_creator(self)
  File "D:\Python\movie_project\venv\lib\site-packages\sqlalchemy\engine\strategies.py", line 114, in connect
    return dialect.connect(*cargs, **cparams)
  File "D:\Python\movie_project\venv\lib\site-packages\sqlalchemy\engine\default.py", line 453, in connect
    return self.dbapi.connect(*cargs, **cparams)
  File "D:\Python\movie_project\venv\lib\site-packages\pymysql\__init__.py", line 94, in Connect
    return Connection(*args, **kwargs)
  File "D:\Python\movie_project\venv\lib\site-packages\pymysql\connections.py", line 325, in __init__
    self.connect()
  File "D:\Python\movie_project\venv\lib\site-packages\pymysql\connections.py", line 599, in connect
    self._request_authentication()
  File "D:\Python\movie_project\venv\lib\site-packages\pymysql\connections.py", line 882, in _request_authentication
    auth_packet = _auth.caching_sha2_password_auth(self, auth_packet)
  File "D:\Python\movie_project\venv\lib\site-packages\pymysql\_auth.py", line 264, in caching_sha2_password_auth
    data = sha2_rsa_encrypt(conn.password, conn.salt, conn.server_public_key)
  File "D:\Python\movie_project\venv\lib\site-packages\pymysql\_auth.py", line 142, in sha2_rsa_encrypt
    raise RuntimeError("cryptography is required for sha256_password or caching_sha2_password")
RuntimeError: cryptography is required for sha256_password or caching_sha2_password
flask-sqlalchemy
3个回答
2
投票

我也有同样的错误

cryptography is required for sha256_password or caching_sha2_password
当我安装“密码学”尝试时它就解决了:

pip install cryptography


0
投票

我知道这个问题的一个可能的解决方案是安装以前版本的mysql,例如v5.7。我觉得还是安装8.0以下的版本比较好。


0
投票

即使安装加密模块后也出现同样的错误

© www.soinside.com 2019 - 2024. All rights reserved.