sqlalchemy.exc.InternalError
来源:11-3 完成最近的礼物(业务的四种编写方案)
慕慕5403139
2018-09-14
model/gift.py
class Gift(Base):
id = Column(Integer, primary_key=True)
user = relationship('User')
uid = Column(Integer, ForeignKey('user.id'))
isbn = Column(String(15), nullable=False)
# book = relationship('Book')
# bid = Column(Integer, ForeignKey('book.id'))
launched = Column(Boolean, default=False)
@property
def book(self):
yushu_book = YuShuBook()
yushu_book.search_by_isbn(self.isbn)
return yushu_book.first
@classmethod
def recent(cls):
recent_gift = Gift.query.filter_by(
launched=False).group_by(
Gift.isbn).order_by(
Gift.create_time).limit(
current_app.config['RECENT_BOOK_COUNT']).distinct().all()
return recent_gift
web/main.py
@web.route('/')
def index():
pass
recent_gifts = Gift.recent()
print(recent_gifts)
books = [BookViewModel(gift.book) for gift in recent_gifts]
return render_template('index.html', recent=books)
错误信息
sqlalchemy.exc.InternalError
sqlalchemy.exc.InternalError: (cymysql.err.InternalError) (1055, "Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'fisher.gift.create_time' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by") [SQL: 'SELECT DISTINCT gift.create_time AS gift_create_time, gift.status AS gift_status, gift.id AS gift_id, gift.uid AS gift_uid, gift.isbn AS gift_isbn, gift.launched AS gift_launched \nFROM gift GROUP BY gift.isbn ORDER BY gift.isbn \n LIMIT %s'] [parameters: (30,)] (Background on this error at: http://sqlalche.me/e/2j85)
我在网上查到的问题是mysql5.7.5之后sql_mode默认值是"only_full_group_by",不能执行group_by查询,这个该如何解决?
写回答
2回答
-
qq_洪伊_0
2019-04-20
my.ini里设置了sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 但无法识别,最后还是临时性的连接数据库里修改set @@global.sql_mode='NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES';
不过有缺点就是关了数据库重启后又要重新设置了!!!!
00 -
Ivanhou
2018-09-21
我觉得代码应该这样写,请大神们斧正
recent_gifts = Gift.query.filter_by( launched=False).order_by( desc(Gift.create_time)).limit( current_app.config["RECENT_BOOK_COUNT"]).distinct(Gift.isbn).all()
022019-04-20
相似问题