Skip to content

Commit 2b8bf9c

Browse files
authored
Remove the use of before_first_request (#218)
2 parents f959951 + 42d8595 commit 2b8bf9c

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

example/app.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ class ExampleModel(db.Model):
2828
value = db.Column(db.String(100), primary_key=True)
2929

3030

31-
@app.before_first_request
32-
def setup():
33-
db.create_all()
34-
35-
3631
@app.route('/')
3732
def index():
3833
app.logger.info("Hello there")
@@ -45,3 +40,7 @@ def redirect_example():
4540
response = redirect(url_for('index'))
4641
response.set_cookie('test_cookie', '1')
4742
return response
43+
44+
45+
with app.app_context():
46+
db.create_all()

test/basic_app.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
app = Flask('basic_app')
77
app.config['SECRET_KEY'] = 'abc123'
88
app.config['SQLALCHEMY_RECORD_QUERIES'] = True
9-
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
9+
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
1010
# This is no longer needed for Flask-SQLAlchemy 3.0+, if you're using 2.X you'll want to define this:
1111
# app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
1212

@@ -23,12 +23,11 @@ class Foo(db.Model):
2323
id = db.Column(db.Integer, primary_key=True)
2424

2525

26-
@app.before_first_request
27-
def setup():
28-
db.create_all()
29-
30-
3126
@app.route('/')
3227
def index():
3328
Foo.query.filter_by(id=1).all()
3429
return render_template('basic_app.html')
30+
31+
32+
with app.app_context():
33+
db.create_all()

0 commit comments

Comments
 (0)