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查询,这个该如何解决?
7月老师深入浅出剖析Flask核心机制,和你一起探讨Python高级编程
了解课程