7
7
700ghz2016-03-28 10:59:39
Python
700ghz, 2016-03-28 10:59:39

Why is SQLAlchemy returning old data from the DB?

Server (512 RAM) with: FLASK + SQLAlchemy (SQLite) -> uWSGI -> Nginx
SQLAlchemy returns different data on query.all command (analogous to SELECT).
Example:
* I add a couple of records to the database (I check that they are really present in the database);
* Loading page: all but new entries are returned. wtf?
* Loading page: all records returned (including new ones). Fine!
* Loading page: all but new entries are returned. wtf?
This continues until I reload the Flask application.
I'll be shocked if someone can help.
Thank you in advance!
Maybe the code will be useful:

DECLARATIVE_BASE = declarative_base()
engine = create_engine('sqlite:///database.db')
Session = sessionmaker(bind=engine)
session = Session()



class Order(DECLARATIVE_BASE):

__tablename__ = 'orders'
__table_args__ = (
    {'mysql_engine': 'InnoDB', 'sqlite_autoincrement': True, 'mysql_charset': 'utf8'}
)

id = Column(INTEGER, autoincrement=True, primary_key=True, nullable=False)  # pylint: disable=invalid-name
name = Column(TEXT, nullable=False)
address = Column(TEXT)
phone = Column(TEXT, nullable=False)
email = Column(TEXT)
comment = Column(TEXT)
totalPrice = Column(DECIMAL(asdecimal=False))
orderItems = relationship(Orderitem)
time = Column(TEXT, default=time.strftime("%H:%m %d.%m.%y"))

def __repr__(self):
    return self.__str__()

def __str__(self):
    return "<Order(%s)>" % self.__dict__


@app.route('/api/orders', methods=['GET'])
def getAllOrders():
    allOrders = session.query(Order).all()
    return json.dumps(allOrders, cls=new_alchemy_encoder(False, ['orderItems', 'product']), check_circular=False, ensure_ascii=False) #ensure_ascii=False -- for rigth out cyrlic;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
leclecovich, 2016-03-28
@700ghz

You need to expir the session in your case: docs.sqlalchemy.org/en/rel_1_0/orm/session_state_m...
Or create a session object for each request.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question