请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

sqlalchemy.exc.InternalError

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查询,这个该如何解决?

正在回答 回答被采纳积分+3

插入代码

2回答

qq_洪伊_0 2019-04-20 23:50:19

my.ini里设置了sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 但无法识别,最后还是临时性的连接数据库里修改set @@global.sql_mode='NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES';

不过有缺点就是关了数据库重启后又要重新设置了!!!!

0 回复 有任何疑惑可以回复我~
Ivanhou 2018-09-21 14:27:45

我觉得代码应该这样写,请大神们斧正

1
2
3
4
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()


0 回复 有任何疑惑可以回复我~
  • danzzzz #1
    我也遇到了这个错误,用了你的这个就可以了,谢谢大佬。
    回复 有任何疑惑可以回复我~ 2019-03-27 16:35:20
  • 你这个代码没解决 不重复的需求,输出有重复的
    回复 有任何疑惑可以回复我~ 2019-04-20 23:07:54
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信