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

  • Home
  • SEARCH
  • 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 6707287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:37:17+00:00 2026-05-26T07:37:17+00:00

I am facing some odd problems while learning SQLAlchemy. I am using 0.7.2 version

  • 0

I am facing some odd problems while learning SQLAlchemy. I am using 0.7.2 version of it. These are my test classes:

    class User(Base):
        __tablename__ = 'users'

        id = Column('user_id',Integer,primary_key = True)
        name = Column('user_name',String(20))

        addresses = relationship("Address",backref="user")

        def __repr__(self):
              return "<User(%s)>" % self.name

    class Address(Base):
          __tablename__ = 'addresses'

          id = Column('adress_id',Integer,primary_key = True)
          address = Column('address',String(30))
          user_id = Column('user_id',ForeignKey('users.user_id'))

          def __repr__(self):
               return "<Address(%s)>" % self.address
  1. I accidentally ran my main file twice which made following code run twice

    Session = sessionmaker()
    session = Session(bind=engine)
    q=session.query(User)
    a=q.get(3)
    a.addresses.append(Address(address='myaddress'))
    session.flush()
    print a.addresses[0].id,a.addresses[0].address
    

    I initially thought that a.addresses would have got two address object with ‘myaddress’. But actually there is only one object whose address_id got updated. I mean to say when I ran file first it printed

    1,'myaddress'
    
    and then in second run it printed
    
    2,'myaddress'
    

I confirmed existence of only Address object by printing a.addresses and it shows only object in it.

Should it not add a second Address object in a.addresses on append instead of updating id of existing address object?

Note there is no session.commit() command executed. I simply ran the file twice and noticed this behaviour.

Edit : Added code which creates new session and is responsible for the problem as suggested by van below in the answer

  • 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-26T07:37:18+00:00Added an answer on May 26, 2026 at 7:37 am

    This cannot be seen in your code, but I assume that the code that is executed twice is also executed within two different sessions. This would explain all the side effects you observe:

    • only one address: because second session is not aware of the first one (not commit-ed)
    • address ID is increased: this happens because of flush() in the first session where the identifier is assigned to the object. This is not an update to the previous Address though, but rather an ID assignment to the new Address.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am facing some issues while serializing objects (I am using JBoss Drools, and
I'm facing some trouble when using the class QNetworkAccessManager . I use it to
I'm facing some troubles still while learning, so I guess it tends to get
I am facing some problem while returning data from web services. I am using
I am still facing some problems when using LINQ-to-SQL. I am also looking for
Hi: I'm Facing some problems while scanning for bluetooth devices on a HTC Desire
I am developing android push notification application using C2DM,I am facing some problems in
Im facing some problems, I looked around in the forum and didnt find any
I'm trying to deploy an application on Azure but I'm facing some problems. on
I'm working using scriptaculous library. However I'm facing some issues with inclusion of the

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.