本人主做 iOS 开发,自学 flask, 前段时间做了这个小应用,使用 flask_restful 写了后台,flask_admin 做后台管理,项目稳定运行了好几个月,但是在一个新版本更新后,点击 flask_admin 后台管理界面的时偶尔触发一个 pymysql 错误,非必现,后台查看错误日志如下:
Python main interpreter initialized at 0x14a1ed0
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
"acgpicture.log" [readonly] 26068L, 5473340C 3,1 Top
conn.query(q)
File "/home/ubuntu/ACGPictureFlask/ACGPicture_Flask/venv/lib/python3.6/site-packages/pymysql/connections.py", line 893, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/home/ubuntu/ACGPictureFlask/ACGPicture_Flask/venv/lib/python3.6/site-packages/pymysql/connections.py", line 1103, in _read_query_result
result.read()
File "/home/ubuntu/ACGPictureFlask/ACGPicture_Flask/venv/lib/python3.6/site-packages/pymysql/connections.py", line 1396, in read
first_packet = self.connection._read_packet()
File "/home/ubuntu/ACGPictureFlask/ACGPicture_Flask/venv/lib/python3.6/site-packages/pymysql/connections.py", line 1059, in _read_packet
packet.check_error()
File "/home/ubuntu/ACGPictureFlask/ACGPicture_Flask/venv/lib/python3.6/site-packages/pymysql/connections.py", line 384, in check_error
err.raise_mysql_exception(self._data)
File "/home/ubuntu/ACGPictureFlask/ACGPicture_Flask/venv/lib/python3.6/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
sqlalchemy.exc.InternalError: (pymysql.err.InternalError) (1054, "Unknown column 'FALSE' in 'order clause'") [SQL: 'SELECT user.id AS user_id, user.email AS user_email, user.password AS user_password, user.telephone AS user_telephone, user.username AS user_username, user.`headImageUrl` AS `user_headImageUrl`, user.is_vip AS user_is_vip, user.vip_grade AS user_vip_grade, user.pic_package AS user_pic_package, user.`deadLineTime` AS `user_deadLineTime`, user.`adsID` AS `user_adsID`, user.uuid AS user_uuid, user.creat_time AS user_creat_time, user.login_time AS user_login_time, user.last_login_time AS user_last_login_time, user.vip_id AS user_vip_id \nFROM user ORDER BY false DESC \n LIMIT %(param_1)s'] [parameters: {'param_1': 20}] (Background on this error at: http://sqlalche.me/e/2j85)
[pid: 25290|app: 0|req: 19246/21506] 222.211.233.18 () {46 vars in 1022 bytes} [Fri Jan 18 09:38:39 2019] GET /admin/user/ => generated 291 bytes in 10 msecs (HTTP/1.1 500) 2 headers in 84 bytes (1 switches on core 1)
该界面实现代码如下:
class UserView(ModelView):
def is_accessible(self):
return login.current_user.is_authenticated
column_list = ('id', 'email', 'pic_package', 'is_vip', 'creat_time', 'last_login_time')
def date(v, c, m, p):
return time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime(m.last_login_time))
column_formatters = dict(last_login_time=date)
column_searchable_list = ('email', 'username', 'uuid')
column_default_sort = ('is_vip', True)
def __init__(self, session, **kwargs):
super(UserView, self).__init__(User, session, **kwargs)
用户的模型代码如下:
class User(db.Model):
__tablename__ = 'user'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
email = db.Column(db.String(50), nullable=True)
password = db.Column(db.String(100), nullable=False)
telephone = db.Column(db.String(11), nullable=True)
username = db.Column(db.String(50), nullable=True)
headImageUrl = db.Column(db.String(200), nullable=True, comment="头像")
pictures = db.relationship('Picture',
secondary=user_pictures,
backref=db.backref('users', lazy='dynamic'),
lazy='dynamic')
is_vip = db.Column(db.Boolean, nullable=False,
default=False, comment="是否 Vip")
vip_grade = db.Column(db.Integer, nullable=False,
default=VipType.NoVipType.value, comment="会员等级")
pic_package = db.Column(db.Text, nullable=True)
deadLineTime = db.Column(db.String(200), default=None, comment="过期时间")
adsID = db.Column(db.String(100), nullable=True, default=ADSID)
uuid = db.Column(db.String(50), nullable=False, comment="uuid")
creat_time = db.Column(db.DateTime, default=datetime.now)
login_time = db.Column(db.Integer)
last_login_time = db.Column(db.Integer)
vip_id = db.Column(db.Integer, db.ForeignKey('vip.id'))
def check_password_email(password, checkPassword):
return (password == md5(checkPassword))
def get(self, id):
return self.query.filter_by(id=id).first()
def add(user):
db.session.add(user)
return session_commit()
def update(self):
return session_commit()
def delete(self, id):
self.query.filter_by(id=id).delete()
return session_commit()
@property
def is_authenticated(self):
return True
@property
def is_active(self):
return True
@property
def is_anonymous(self):
return False
def get_id(self):
return self.id
谷歌好久之后都没有结果,望大神不吝赐教,感激不尽!
python 版本: 3.6.5
Flask==1.0.2
Flask-Admin==1.5.1
Flask-RESTful==0.3.6
Flask-Script==2.0.6
Flask-SQLAlchemy==2.3.2
PyMySQL==0.8.1
SQLAlchemy==1.2.7
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.