Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

model and instance methods: session aware (sqlalchemy)

Tags:

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.

like image 731
blueblank Avatar asked Jun 04 '12 14:06

blueblank


People also ask

What is model in SQLAlchemy?

SQLAlchemy (source code) is a Python library for accessing persistent data stored in relational databases either through raw SQL or an object-relational mapper.

What does Session do in SQLAlchemy?

What does the Session do? One of the core concepts in SQLAlchemy is the Session . A Session establishes and maintains all conversations between your program and the databases. It represents an intermediary zone for all the Python model objects you have loaded in it.

What is Backref in SQLAlchemy?

The sqlalchemy backref is one of the type keywords and it passed as the separate argument parameters which has to be used in the ORM mapping objects. It mainly includes the event listener on the configuration attributes with both directions of the user datas through explicitly handling the database relationships.

Is SQLAlchemy an ORM?

SQLAlchemy is a library that facilitates the communication between Python programs and databases. Most of the times, this library is used as an Object Relational Mapper (ORM) tool that translates Python classes to tables on relational databases and automatically converts function calls to SQL statements.


1 Answers

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.

like image 59
van Avatar answered Oct 13 '22 00:10

van