I\'m creating an application to replace current tab managers in the browser. I\'ve created a simple one-to-many relationship between groups - table Topic, and tabs - table T
Your script is the WSGI application, hence runs as __main__
. If you use debug=True
as argument to app.run()
the server will start, set debug mode: on and restart with stat
, executing the __name__ == '__main__'
block again (and again every time you make changes to your script and save it while the server is running).
When you start your app on an empty/deleted DB, the first time the __name__ == '__main__'
block is executed, it creates the DB and inserts the two Topic
and three Tab
objects you create in that block. Then it sets debug mode and restarts, again executing that block, attempting to insert these five objects a second time.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return "Hello, World!"
if __name__ == '__main__':
print(__name__)
app.run(debug=True)
Output:
__main__ # <-- fist time around it creates DB and objects
* Serving Flask app "main" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Restarting with stat # <-- re-executing your script
__main__ # <-- trying to create the same objects again, violating the UNIQUE constraint
* Debugger is active!
* Debugger PIN: 302-544-855
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)