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 8230157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:02:40+00:00 2026-06-07T17:02:40+00:00

I’ve been using SQLAlchemy to connect to a MySQL database. It has been working

  • 0

I’ve been using SQLAlchemy to connect to a MySQL database. It has been working good up to now, but I’ve run into some trouble. I wanted to call a method on one of my classes after it was queried which would assign a few new variables. The trouble was that only a single user per query would end up having the variables set by my ‘on_query’ method.

...stuff setting up a connection to the database as well as a SQL session.
   All were done using default values.

class UserTest(Base):
    __tablename__ = "testtable"

    ID = Column(Integer(unsigned=True),
                primary_key=True, nullable=False, autoincrement=True)

    def __init__(self):
        pass

    def on_query(self):
        self.foo = "bar"

query = session.query(UserTest)

for u in query:
    u.on_query()

print [hasattr(u, "foo") for u in query]
#prints [False, False, False, False, False, False, False, False, False, True]

session.close()

I discovered that if I were to create a list by iterating the query, the ‘on_query’ method would work as I intended it to.

users = [u for u in query]
for u in users:
    u.on_query()
print [hasattr(u, "foo") for u in users]
#prints [True, True, True, True, True, True, True, True, True, True]

What is the cause of this? And is there a way to stop it from removing the ‘foo’ variable each iteration? I’d prefer to keep the Users inside of the query object if possible.

  • 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-06-07T17:02:41+00:00Added an answer on June 7, 2026 at 5:02 pm

    In the first case, the query is actually spawned twice, once by the for-loop, and once by the print statement.

    Since the for-loop keeps replacing the u reference, at the end of the loop, only the last one remains in SQLAlchemy session. You can verify this with print session.identity_map.

    When the query is spawned for the second time by the print statement, all except the last one has to be retrieved from the database again, while the last one is retrieved directly from SQLAlchemy session, thus having the foo attribute set.

    EDIT: One way to do this more transparently is by using reconstructor, e.g.

    class UserTest(Base):
        ...
    
        @orm.reconstructor
        def init_on_load(self):
            self.foo = "bar"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has

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.