Giter VIP home page Giter VIP logo

Comments (7)

miguelgrinberg avatar miguelgrinberg commented on September 8, 2024

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.

mgraupner avatar mgraupner commented on September 8, 2024

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.

miguelgrinberg avatar miguelgrinberg commented on September 8, 2024

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.

mgraupner avatar mgraupner commented on September 8, 2024

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.

miguelgrinberg avatar miguelgrinberg commented on September 8, 2024

Where in the code exactly are you printing test?

from flasky.

mgraupner avatar mgraupner commented on September 8, 2024

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.

mgraupner avatar mgraupner commented on September 8, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.