将结果 session.query 转换为对象
我从 SQLAlchemy 开始,尝试获取最后一个插入(一条推文),但我只得到 sql 查询:
conf = readConf("../utils/conf.yaml")
schema = conf['bdd']['type'] + '://' + conf['bdd']['user'] + ':' + conf['bdd']['password'] + '@' + conf['bdd']['host'] + '/' + conf['bdd']['db'] + '?charset=utf8'
engine = create_engine(schema, echo=True)
Base = getBase()
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
tq = session.query(Tweet).group_by(Tweet.tweet_id_uniq).filter(func.max(Tweet.tweet_id_uniq) == Tweet.tweet_id_uniq)
# tweet_id_uniq is a BIGINT autoincrement, so the highest value is the last Tweet.
print tq
打印结果是 SQL 查询(为了易读性而包装):
SELECT tweets.tweet_id AS tweets_tweet_id,
tweets.tweet_id_uniq AS tweets_tweet_id_uniq,
tweets.user_id AS tweets_user_id,
tweets.user_id_uniq AS tweets_user_id_uniq,
tweets.tweet_text AS tweets_tweet_text,
tweets.created_at AS tweets_created_at,
tweets.in_reply_to AS tweets_in_reply_to,
tweets.geo_lat AS tweets_geo_lat,
tweets.geo_long AS tweets_geo_long,
tweets.screen_name AS tweets_screen_name,
tweets.name AS tweets_name,
tweets.profile_image_url AS tweets_profile_image_url,
tweets.source AS tweets_source
FROM tweets
WHERE max(tweets.tweet_id_uniq) = tweets.tweet_id_uniq
GROUP BY tweets.tweet_id_uniq
为什么我不检索一条推文?
编辑:如果我添加 .one() 或 .fisrt() 或 .all(),我会收到此错误:
2012-01-31 16:05:37,644 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2012-01-31 16:05:37,645 INFO sqlalchemy.engine.base.Engine SELECT tweets.tweet_id AS tweets_tweet_id, tweets.tweet_id_uniq AS tweets_tweet_id_uniq, tweets.user_id AS tweets_user_id, tweets.user_id_uniq AS tweets_user_id_uniq, tweets.tweet_text AS tweets_tweet_text, tweets.created_at AS tweets_created_at, tweets.in_reply_to AS tweets_in_reply_to, tweets.geo_lat AS tweets_geo_lat, tweets.geo_long AS tweets_geo_long, tweets.screen_name AS tweets_screen_name, tweets.name AS tweets_name, tweets.profile_image_url AS tweets_profile_image_url, tweets.source AS tweets_source
FROM tweets
WHERE max(tweets.tweet_id_uniq) = tweets.tweet_id_uniq
2012-01-31 16:05:37,645 INFO sqlalchemy.engine.base.Engine ()
...
File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.7.3-py2.6-linux-i686.egg/sqlalchemy/engine/default.py", line 330, in do_execute
cursor.execute(statement, parameters)
File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 166, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 35, in defaulterrorhandler
raise errorclass, errorvalue
sqlalchemy.exc.ProgrammingError: (ProgrammingError) (1111, 'Invalid use of group function') 'SELECT tweets.tweet_id AS tweets_tweet_id, tweets.tweet_id_uniq AS tweets_tweet_id_uniq, tweets.user_id AS tweets_user_id, tweets.user_id_uniq AS tweets_user_id_uniq, tweets.tweet_text AS tweets_tweet_text, tweets.created_at AS tweets_created_at, tweets.in_reply_to AS tweets_in_reply_to, tweets.geo_lat AS tweets_geo_lat, tweets.geo_long AS tweets_geo_long, tweets.screen_name AS tweets_screen_name, tweets.name AS tweets_name, tweets.profile_image_url AS tweets_profile_image_url, tweets.source AS tweets_source \nFROM tweets \nWHERE max(tweets.tweet_id_uniq) = tweets.tweet_id_uniq GROUP BY tweets.tweet_id_uniq' ()
I start with SQLAlchemy and i try to get my last insert (a Tweet) but i only get the sql query :
conf = readConf("../utils/conf.yaml")
schema = conf['bdd']['type'] + '://' + conf['bdd']['user'] + ':' + conf['bdd']['password'] + '@' + conf['bdd']['host'] + '/' + conf['bdd']['db'] + '?charset=utf8'
engine = create_engine(schema, echo=True)
Base = getBase()
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
tq = session.query(Tweet).group_by(Tweet.tweet_id_uniq).filter(func.max(Tweet.tweet_id_uniq) == Tweet.tweet_id_uniq)
# tweet_id_uniq is a BIGINT autoincrement, so the highest value is the last Tweet.
print tq
The print result is the SQL query (wrapped for legibility):
SELECT tweets.tweet_id AS tweets_tweet_id,
tweets.tweet_id_uniq AS tweets_tweet_id_uniq,
tweets.user_id AS tweets_user_id,
tweets.user_id_uniq AS tweets_user_id_uniq,
tweets.tweet_text AS tweets_tweet_text,
tweets.created_at AS tweets_created_at,
tweets.in_reply_to AS tweets_in_reply_to,
tweets.geo_lat AS tweets_geo_lat,
tweets.geo_long AS tweets_geo_long,
tweets.screen_name AS tweets_screen_name,
tweets.name AS tweets_name,
tweets.profile_image_url AS tweets_profile_image_url,
tweets.source AS tweets_source
FROM tweets
WHERE max(tweets.tweet_id_uniq) = tweets.tweet_id_uniq
GROUP BY tweets.tweet_id_uniq
Why don't I retrieve a Tweet ?
EDIT : If I add .one() or .fisrt() or .all(), i get this error :
2012-01-31 16:05:37,644 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2012-01-31 16:05:37,645 INFO sqlalchemy.engine.base.Engine SELECT tweets.tweet_id AS tweets_tweet_id, tweets.tweet_id_uniq AS tweets_tweet_id_uniq, tweets.user_id AS tweets_user_id, tweets.user_id_uniq AS tweets_user_id_uniq, tweets.tweet_text AS tweets_tweet_text, tweets.created_at AS tweets_created_at, tweets.in_reply_to AS tweets_in_reply_to, tweets.geo_lat AS tweets_geo_lat, tweets.geo_long AS tweets_geo_long, tweets.screen_name AS tweets_screen_name, tweets.name AS tweets_name, tweets.profile_image_url AS tweets_profile_image_url, tweets.source AS tweets_source
FROM tweets
WHERE max(tweets.tweet_id_uniq) = tweets.tweet_id_uniq
2012-01-31 16:05:37,645 INFO sqlalchemy.engine.base.Engine ()
...
File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.7.3-py2.6-linux-i686.egg/sqlalchemy/engine/default.py", line 330, in do_execute
cursor.execute(statement, parameters)
File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 166, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 35, in defaulterrorhandler
raise errorclass, errorvalue
sqlalchemy.exc.ProgrammingError: (ProgrammingError) (1111, 'Invalid use of group function') 'SELECT tweets.tweet_id AS tweets_tweet_id, tweets.tweet_id_uniq AS tweets_tweet_id_uniq, tweets.user_id AS tweets_user_id, tweets.user_id_uniq AS tweets_user_id_uniq, tweets.tweet_text AS tweets_tweet_text, tweets.created_at AS tweets_created_at, tweets.in_reply_to AS tweets_in_reply_to, tweets.geo_lat AS tweets_geo_lat, tweets.geo_long AS tweets_geo_long, tweets.screen_name AS tweets_screen_name, tweets.name AS tweets_name, tweets.profile_image_url AS tweets_profile_image_url, tweets.source AS tweets_source \nFROM tweets \nWHERE max(tweets.tweet_id_uniq) = tweets.tweet_id_uniq GROUP BY tweets.tweet_id_uniq' ()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在尝试匹配最大的值,然后将其分组。这个价值是独一无二的。所以你不妨直接订购并购买一份。
tq 现在是具有最大 id 的 Tweet 对象。
顺便说一下,问题是你的查询结果是不正确的sql。如果不重新包含该表(这可能不是您想要做的)或使用子查询(也很尴尬),您就无法将最大值与同一表中的列进行匹配。
You are trying to match on the greatest value and then group it. That value is unique. So you might as well just order by and get one.
tq is now your Tweet object with the largest id.
Incidentally, the problem is that your resulting query is incorrect sql. You can't match a max value against a column from that same table without re-including the table (which probably isn't what you want to do) or using a subquery (also awkward).
试试这个:
session.query(Tweet).group_by(Tweet.tweet_id_uniq).filter(func.max(Tweet.tweet_id_uniq) == Tweet.tweet_id_uniq).all()
或 <代码>first(),或
one()
。Try this:
session.query(Tweet).group_by(Tweet.tweet_id_uniq).filter(func.max(Tweet.tweet_id_uniq) == Tweet.tweet_id_uniq).all()
or
first()
, orone()
.