2012-01-31 40 views
2

我先從SQLAlchemy的,我試圖讓我的最後插入(鳴叫),但我只得到SQL查詢:變換結果session.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 

打印結果的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 

爲什麼我不檢索Tweet?

編輯:如果我加入。一()或.fisrt()或。所有(),我得到這個錯誤:

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'() 

回答

2

您試圖匹配最大的值,然後對其進行分組。該值是獨一無二的。所以你可能只需點一下就可以得到一個。

tq = session.query(Tweet).order_by(desc(Tweet.tweet_id_uniq)).first() 

tq現在是您的Tweet對象的最大ID。

順便說一句,問題是您的結果查詢是不正確的SQL。如果不重新包括表(這可能不是您想要做的)或使用子查詢(也很尷尬),您無法將最大值與同一表中的列進行匹配。

+0

它的工作原理。謝謝你的解釋。你的解決方案確實更加優雅。 – Yohann 2012-01-31 20:29:38

1

試試這個: session.query(Tweet).group_by(Tweet.tweet_id_uniq).filter(func.max(Tweet.tweet_id_uniq) == Tweet.tweet_id_uniq).all()

first(),或one()

+0

在編輯中查看答案。 – Yohann 2012-01-31 15:02:03