I am working on an new project which is already developed in Flask and I have no knowledge of Flask. My company gave the project to me because I have Django experience.
This is the structure of the project:
models
-db.py
-model1.py
-model2.py
- ..
static
- ..
templates
- ..
myapp.py
myapp.py
contains all config files and server init code with all other functionality such as that for home page and sign up page.
When I run myapp.py
it runs OK, but the tables are not created automatically (I found that first migration has to be done). I have no idea how to do it.
The project makes use of postgresql and SQLAlchemy form flask_sqlalchemy Modules.
db.py
file:
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
All models have from db import db
myapp
file:
# ===================================================================
# SQL ALCHEMY
# ===================================================================
if (SERVER_MODE == RUN_MODE.PRODUCTION):
app.config['SQLALCHEMY_DATABASE_URI'] = (
os.environ["DATABASE_URL"]
)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
else:
app.config['SQLALCHEMY_DATABASE_URI'] = (
'postgresql://' +
'creathiveswebapp:creathives' +
'@' +
'localhost/cl_creathives_pgdb'
)
app.config['SQLALCHEMY_ECHO'] = False
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db.init_app(app)
and
...
# ===================================================================
# START SERVER
# ===================================================================
if __name__ == "__main__":
port = int(os.environ.get('PORT', 5000))
if (SERVER_MODE == RUN_MODE.PRODUCTION):
# TODO: Turn off debug
app.run(host='0.0.0.0', port=port, debug=True)
else:
app.run(host='0.0.0.0')
How do I make first migration to create the tables.
Use this command :
python manage.py db migrate
And for database migration settings,Try something like this :
import os
from flask.ext.script import Manager
from flask.ext.migrate import Migrate, MigrateCommand
from app import app, db
app.config.from_object(os.environ['APP_SETTINGS'])
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
manager.run()
For further knowledge,Read from here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With