最近使用sqlalchemy和pymysql连接数据库出现了如下错误:
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (1055, "Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'hotel.room_info.network_reference_hotel_room_type' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by")
原因是由于sqlchemy和pymysql库的版本不适配MySQL版本,解决方法为:
降低MySQL版本
提高sqlalchemy和pymsyql版本
修改MySQL sql_mode 变量为非only_full_group_by模式,具体做法:
进入mysql配置文件,在mysqld下面添加以下代码:
# 设置sql_mode,关闭ONLY_FULL_GROUP_BY,避免使用group by函数导致1055错误
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
保存后重启mysql服务:
net stop mysql
net start mysql
然后查看sql_mode变量:
show variables like '%sql_mode%';
出现的变量值中没有only_full_group_by的字段说明修改正确。