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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:11:32+00:00 2026-06-05T02:11:32+00:00

So, using and learning with sqlalchemy. I have an instance, I need to get

  • 0

So, using and learning with sqlalchemy.

I have an instance, I need to get a value. If that value exists, return it.
If not calculate and return it.

Invariably someone will say ‘you’re doing it wrong’ and input on improvement is appreciated in general.
However I am looking into how I can do this without explicitly having to manage the session, because what I’m working on is starting to grow, and constantly managing the session for when I want to update an instance is problematic. It makes me think that I am in fact doing it wrong.

So how do I fix the method below to not have manage the session explicitly?

def method(self, session):
        if self.i_needed_this is None:
            self.i_needed_this = calculate(calcutron)
            session.add(self)
            session.commit()
            return self.i_needed_this                        
        else:
            return self.i_needed_this

Maybe this question should be titled ‘making instances session aware so I’m not always explicitly managing it’, and if it’s a dumb question, at least show me why with examples and point me to where others have asked better.

Edit: apparently, importing the session I’m using works, and it is available, so maybe its a non issue or a future one for when I am more skilled with sqlalchemy.

  • 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-05T02:11:34+00:00Added an answer on June 5, 2026 at 2:11 am

    Your question is so common, that the answer to it is in the Session Frequently Asked Questions of SA documentation:

    • How can I get the Session for a certain object?
      Use the object_session() classmethod available on Session:
      session = Session.object_session(someobject)

    However, it assumes the object is persistent and is either new (but added to a session already) or bound to a session. Your sample code contains logic to add the object to the session, so my assumption might not be true in this case.

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

Sidebar

Related Questions

I have a liquid layout and am learning/ using Less.js for my CSS. I
So I have started learning directx11 using a book by Frank D.Luna. His examples
Good afternoon, I'm learning about using RegEx's in Ruby, and have hit a point
I have a strong background in PHP / ZEND and I'm now using learning
I have been using the Learning Java 2nd Edtion book to try make my
I have an annoying problem when using machine learning library PyML . PyML uses
I am continually studying up on languages I have been using and learning, as
I have a small test database I'm using for learning SQL. There's a Duel
so I'm using Weka Machine Learning Library's JAVA API and I have the following
I am interested in using/learning RoR in a project where I have to use

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.