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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:26:22+00:00 2026-06-12T15:26:22+00:00

I have a table and populating it with object list then I need to

  • 0

I have a table and populating it with object list then I need to use their IDs, but I am getting an

Instance <location at 0x457f3b0> is not bound to a Session; attribute refresh operation cannot proceed

error.

I am populating a list with objects and send it to a function to insert all at once. Then I try to use IDs.

Here is my insert all function:

def insertlocations(locationlist):
    session.add_all(locationlist)
    session.commit()
    session.close()

then I try to get IDs:

insertlocations(neighbourhoodlist)
session.flush(neighbourhoodlist)
for neighbourhood in neighbourhoodlist:
    print neighbourhood.locationid

Session is global by the way. Any further info needed?

The data are inserted, as I look in the MySQL table.

  • 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-12T15:26:24+00:00Added an answer on June 12, 2026 at 3:26 pm

    Most likely your problem is that you already close() the session in your insertlocations() function.

    When you then access neighbourhood.locationid, the session is closed and thatneighbourhood object isn’t bound to a session any more.

    For example, this should work:

    from sqlalchemy import create_engine
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy import Column, Integer, String
    from sqlalchemy.orm import sessionmaker
    
    engine = create_engine('sqlite:///example.db')
    engine.echo = True
    Base = declarative_base()
    
    class Location(Base):
        __tablename__ = 'locations'
    
        locationid = Column(Integer, primary_key=True)
        name = Column(String)
        address = Column(String)
    
        def __init__(self, name, address):
            self.name = name
            self.address = address
    
    
    Base.metadata.create_all(engine)
    
    Session = sessionmaker(bind=engine)
    session = Session()
    
    def insertlocations(locationlist):
        session.add_all(locationlist)
        session.commit()
    
    
    loc1 = Location('loc1', 'Foostreet 42')
    loc2 = Location('loc2', 'Barstreet 27')
    
    neighbourhoodlist = [loc1, loc2]
    
    insertlocations(neighbourhoodlist)
    for neighbourhood in neighbourhoodlist:
        print neighbourhood.locationid
    
    session.close()
    
    • Move session.close() out of your function and do it after you’re done using that session.
    • Ditch the session.flush(), it’s not needed since you already commit the session when you add the objects.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a feed that is populating a single text field in a table
I have table in which I am inserting rows for employee but next time
I'm parsing a json object and populating it in table view. When the table
I have a javascript object with several properties. The object also contains an instance
I have one package contains many Stored Procedure for populating diffrent tables. This package
does Android have a best practices guideline on creating & populating the db/tables programmatically
I have table with around 70 000 rows. There is 6000 rows that i
I have table and this table contain result column with some entries. I just
I have table with 300 000 records (MyISAM). I get record from this table
I have table of data that is sorted as follows: Item | Sample |

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.