Why db.session.remove() must be called?
<p>I'm following a tutorial to learn flask web developing, and here is its unit testing file:</p>

<pre><code>import unittest
from flask import current_app
from app import create_app, db

class BasicsTestCase(unittest.TestCase):
    def setUp(self):
        self.app = create_app('testing')
        self.app_context = self.app.app_context()
        self.app_context.push()
        db.create_all()

    def tearDown(self):
        db.session.remove()
        db.drop_all()
        self.app_context.pop()

    def test_foo(self):
        pass
</code></pre>

<p>Also, I've found these sentences in SQLAlchemy document:</p>

<blockquote>
  <p>Using the above flow, the process of integrating the <code>Session</code> with
  the web application has exactly two requirements:</p>
  
  <ul>
  <li><p>......</p></li>
  <li><p>Ensure that <code>scoped_session.remove()</code> is called when the web request ends, usually by integrating with the web frameworkâs event system to
  establish an âon request endâ event.</p></li>
  </ul>
</blockquote>

<p>My question is: Why do I need to call <code>db.session.remove()</code>?</p>

<p>I think as long as <code>db.session.commit()</code> is not invoked, the database won't be modified. Also, when I comment out this line, the application will still be able to pass the unit test. (In the real code, I have more reasonable test cases, rather than just <code>test_foo</code>)</p>

<p>I've consulted the documents of both Flask-SQLAlchemy and SQLAlchemy, but the former doesn't even mention <code>db.session.remove()</code>, while the latter is too abstract for me to understand.</p>

<p>I haven't understand why <code>db.session.remove()</code> is necessary until I inspected the whole project:</p>

<p>This is because in <code>config.py</code>, <code>SQLALCHEMY_COMMENT_ON_TEARDOWN</code> is set to <code>True</code>. As a result, changes made to <code>db.session</code> would be auto-commited if <code>db.session</code> isn't destroyed.</p>

python session flask sqlalchemy flask-sqlalchemy