2

This is my first question on stackoverflow, so please correct me if I do something wrong :).

My data from a database hosted at Google Cloud SQL is caching with Flask-SQLAlchemy. When I add a new record and try to get that record it doesn't exist.

I am using a script that adds records and I use one to get records.

I first tried it with SQLite, that worked perfectly. But with MySQL at Google Cloud SQL it doesn't.

Every time I add/change something to the database I use db.session.commit()

I use the pymysql module with this: pymysql.install_as_MySQLdb() And my connection URI looks like this: mysql://...

Edit: This is what I use to add a new record (my script is for adding jokes):

new_joke = Jokes(joke, user["username"], user["id"], avatar_url, "0")
db.session.add(new_joke)
db.session.commit()

And this is what I use to get a record (random):

jokes = Jokes.query.all()
randint = random.randint(0, len(jokes) - 1)
joke = jokes[randint]

1 Answers1

1

I found an answer!

By doing db.session.commit() before making a query, it refreshes it's cache.

My code now looks like this:

db.session.commit()
jokes = Jokes.query.all()
randint = random.randint(0, len(jokes) - 1)
joke = jokes[randint]