|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码如下:from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "mysql://root:rootroot@localhost:3306/flask"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db = SQLAlchemy(app)
class Role(db.Model):
"""用户角色/身份表"""
__tablename__ = "tbl_roles"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(32), unique=True) # unique 表示不允许出现重复的值
users = db.relationship("User", backref="role")
class User(db.Model):
__tablename__ = "tbl_users"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(63), unique=True)
email = db.Column(db.String(128), unique=True)
password = db.Column(db.String(128))
role_id = db.Column(db.Integer, db.ForeignKey("tbl_roles.id"))
if __name__ == '__main__':
db.drop_all()
db.create_all()
数据库的地址和账户密码都是对的,在创建表的时候一直报错,找不到原因,报错如下:
(venv) D:\flaskProject1>python mysql.py
Traceback (most recent call last):
File "mysql.py", line 31, in <module>
db.drop_all()
File "D:\flaskProject1\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 1047, in drop_all
self._execute_for_all_tables(app, bind, 'drop_all')
File "D:\flaskProject1\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 1031, in _execute_for_all_tables
op(bind=self.get_engine(app, bind), **extra)
File "D:\flaskProject1\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 962, in get_engine
return connector.get_engine()
File "D:\flaskProject1\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 555, in get_engine
options = self.get_options(sa_url, echo)
File "D:\flaskProject1\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 570, in get_options
self._sa.apply_driver_hacks(self._app, sa_url, options)
File "D:\flaskProject1\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 884, in apply_driver_hacks
sa_url.query.setdefault('charset', 'utf8')
AttributeError: 'sqlalchemy.cimmutabledict.immutabledict' object has no attribute 'setdefault'
实在是找不到解决的办法了,请教下各位。
本帖最后由 °蓝鲤歌蓝 于 2021-2-3 00:00 编辑
这个库的版本问题,换个旧一点的版本。
|
|