-1

I just did the same things as a tutorial, his works, but mine failed. It seams like a sqlalchemy internal error which shows in the terminal.

I dont know what's wrong with my code, what I did before that is to install the flask-sqlalthemy,pymysql and flask in my project interpreter in pycharm community settings.

I use the virtrualenev in this project.

the code below

coding: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
5
  • I'm new to SQLAlchemy, but from the error output, I see pymysql.err.InternalError: (1170, "BLOB/TEXT column 'info' used in key specification without a key length"). Try specifying a length info = db.Column(db.Text(600), unique=True), matching the output of your SQL. Commented May 1, 2019 at 14:21
  • I dont know why there is no such an error about the key lenth of the Text.I will update the error, because the error changed. Commented May 1, 2019 at 23:52
  • 1
    RuntimeError: cryptography is required for sha256_password or caching_sha2_password didn't catch your eye at all? Also, after your edit your title does not match your question any longer. You should edit it as well. Commented May 2, 2019 at 0:26
  • sorry, I can see the error shows here, but I dont know what wrong with it. If it shows the error happened in my code, I may fix it by myself. But that looks like happened in the package. Commented May 2, 2019 at 0:59
  • 2
    Does this answer your question? "cryptography is required for sha256_password or caching_sha2_password" Commented Apr 14, 2024 at 6:48

2 Answers 2

2

I had the same error cryptography is required for sha256_password or caching_sha2_password it was solved when i installed "cryptography" try:

pip install cryptography

Sign up to request clarification or add additional context in comments.

Comments

0

I know a possible solution of this question is installing a previous version of mysql, for examle, v5.7. I think it is better to install a version lower than 8.0.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.