'SQLAlchemy lost connection to Database when I am accessing model's property

python 2.7.10 SQLAlchemy 1.2.15

I have a model like this

class SomeClass(Base):
    __tablename__ = 'some_table'
    id = Column(Integer, primary_key=True)
    name =  Column(String(50))

if there will be a lot of time between getting an object and accessing its property I can get an exception

obj = SomeClass.objects.get(11)
sleep(10000) # Some time passed
print(obj.name)

Here I can get an Exception

psycopg2.InterfaceError: connection already closed

Is there any way to avoid this Exception?

UPD: I don't understand why it needs a connection to DB when it tries to get property from obj even if obj was received before???



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source