Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7409289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:03:37+00:00 2026-05-29T06:03:37+00:00

I start with SQLAlchemy and i try to get my last insert (a Tweet)

  • 0

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' ()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:03:38+00:00Added an answer on May 29, 2026 at 6:03 am

    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 = session.query(Tweet).order_by(desc(Tweet.tweet_id_uniq)).first()
    

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I start a .Net server side program on my local workstation, but soon it
Start with your module, utils.coffee: exports.foo = -> exports.bar = -> Then your main
I start a long operation (usually long query (SELECT) to database). It might take
I start to learn asm using fasm, unfortunately after compile code below I get
To start with some context, I'm so annoyed whenever I get on a Windows
I start an IMAGE_CAPTURE Intent like this, and my activity's onActivityResult() get called: Intent
It took me a while, but I figured out how to use SQLAlchemy to
I start my application in landscape on my simulator. But when I start playing
I am using SQLAlchemy with declarative base and Python 2.6.7 to insert data in
Start a new Silverlight application... and in the code behind (in the Loaded event),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.