7

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.

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Hello Prakhar thanks for the answer. I implemented the code but when running "python manage.py db migrate" it raises "File "migrations/env.py", line 23, in <module> target_metadata = current_app.extensions['migrate'].db.metadata AttributeError: 'module' object has no attribute 'metadata'" can you help me with it.
-2
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__':
    app.run()

Use command : flask db migrate -m 'comments' flask db upgrade

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.