Comments (7)
You can certainly do explicit commits if you like, but the choice I've taken with this application is to configure Flask-SQLAlchemy to do the commits for me after each request ends. The option is called SQLALCHEMY_COMMIT_ON_TEARDOWN
. See the docs here, it's the last in the table.
Note that even with this option enabled there are times when an explicit commit is needed. This is typically the case when you are adding a new row to the database and need to have the id of that new row to use within the same request.
from flasky.
Hi Miguel,
I'm experiencing a similiar problem:
The reset_password
function in the user model is working fine in my development setup but after deploying it in production mode the password is not updated. After turning on the SQLALCHEMY_ECHO = True
option I can see that there are no UPDATE statements sent to the database.
What is even more confusing is that when I set FLASK_CONFIG=production
on my development machine and let this configuration connect to the production database the updates are coming in just fine, just not on my server.
Is there a way i can debug this? The server is using the production config and SQLALCHEMY_COMMIT_ON_TEARDOWN = True
is set in the common config so this should all be fine. I’m a little lost right now… The database is MySQL and I'm using the InnoDB database engine.
Kind regards,
Michael
from flasky.
Have you verified that User.reset_password
executes, yet you don't get an update? Add a print
statement right before db.session.add(self)
to ensure the model is being changed.
from flasky.
Yes, the method is called, I put several debug messages into my code. Following is the log:
[2015-02-05 16:38:16 +0000] [22237] [DEBUG] POST /auth/reset/eyJhbGciOiJIUzI1NiIsxxxxxxxxxxxQyMzE1Mzg0OSwiaWF0IjoxNDIzMTUwMjQ5fQ.eyJyZXNldCI6MTJ9.7wC-R5XaGrYdflZhoag3KVqP_ZO9km2oWMSfe1k8f6c
2015-02-05 16:38:16,980 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'sql_mode'
2015-02-05 16:38:16,980 INFO sqlalchemy.engine.base.Engine ()
2015-02-05 16:38:16,981 INFO sqlalchemy.engine.base.Engine SELECT DATABASE()
2015-02-05 16:38:16,982 INFO sqlalchemy.engine.base.Engine ()
2015-02-05 16:38:16,983 INFO sqlalchemy.engine.base.Engine show collation where `Charset` = 'utf8' and `Collation` = 'utf8_bin'
2015-02-05 16:38:16,983 INFO sqlalchemy.engine.base.Engine ()
2015-02-05 16:38:16,985 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS CHAR(60)) AS anon_1
2015-02-05 16:38:16,985 INFO sqlalchemy.engine.base.Engine ()
2015-02-05 16:38:16,986 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS CHAR(60)) AS anon_1
2015-02-05 16:38:16,986 INFO sqlalchemy.engine.base.Engine ()
2015-02-05 16:38:16,987 INFO sqlalchemy.engine.base.Engine SELECT CAST('test collated returns' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin AS anon_1
2015-02-05 16:38:16,987 INFO sqlalchemy.engine.base.Engine ()
2015-02-05 16:38:16,988 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2015-02-05 16:38:16,989 INFO sqlalchemy.engine.base.Engine SELECT users.id AS users_id, users.email AS users_email, users.password_hash AS users_password_hash, users.name AS users_name, users.`firstName` AS `users_firstName`, users.confirmed AS users_confirmed, users.member_since AS users_member_since, users.last_seen AS users_last_seen, users.is_consultant AS users_is_consultant
FROM users
WHERE users.email = %s
LIMIT %s
2015-02-05 16:38:16,990 INFO sqlalchemy.engine.base.Engine ('[email protected]', 1)
2015-02-05 16:38:16,993 INFO sqlalchemy.engine.base.Engine SELECT users.id AS users_id, users.email AS users_email, users.password_hash AS users_password_hash, users.name AS users_name, users.`firstName` AS `users_firstName`, users.confirmed AS users_confirmed, users.member_since AS users_member_since, users.last_seen AS users_last_seen, users.is_consultant AS users_is_consultant
FROM users
WHERE users.email = %s
LIMIT %s
2015-02-05 16:38:16,993 INFO sqlalchemy.engine.base.Engine ('[email protected], 1)
test
2015-02-05 16:38:17,009 INFO sqlalchemy.engine.base.Engine ROLLBACK
Just before the rollback you can see the new password. But there is no UPDATE
statement.
from flasky.
Where in the code exactly are you printing test
?
from flasky.
Just before db.session.add(self)
def reset_password(self, token, new_password):
s = Serializer(current_app.config['SECRET_KEY'])
try:
data = s.loads(token)
except:
return False
if data.get('reset') != self.id:
return False
self.password = new_password
print new_password
db.session.add(self)
return True
from flasky.
So, at least I could mitigate the problem:
I added some debug messages to Flask-SQLAlchemy and got the following error message in the response_or_exc
object of the @teardown function: [Errno 11] Resource temporarily unavailable
After some googling I found the following solution: http://serverfault.com/a/606371 After switching from sync worker to gevent worker in unicorn everything works as expected...
I don't know where this error is coming from but it seems gunicorn is responsible. I will keep you updated what the real cause of this is.
from flasky.
Related Issues (20)
- Mail from must equal authorized user? HOT 2
- The app has no db file to initialize the database HOT 4
- Breaks in python 3.10.6 HOT 6
- Update Read me. HOT 1
- Flask mail returning mail as exception HOT 1
- Selenium testing failing HOT 3
- Question on before_request and unconfirmed routes. HOT 4
- While testing client requests, how get/post by endpoint instead of url? HOT 2
- Used flask_ldap3_login? HOT 2
- no .env and .env-mysql file when I run docker-compose HOT 7
- Hello world
- html inheritance on tag 3b
- Flask-WTF Question HOT 2
- Redirect issue in Example 8-23 HOT 4
- HTML Syntax Issue in 2nd Edition of Book HOT 1
- Example 14-4 Question in 2nd Edition HOT 4
- A 'SQLAlchemy' instance has already been registered on this Flask app. Import and use that instance instead. HOT 1
- CSRF session token is missing
- Flask run command doesn't work HOT 6
- SMTP Handler - No Emails HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flasky.