0

I was having difficulty migrating updates to my table model using Flask-SQLALchemy library

After making changes to my table schema, I ran the migrate command and the response suggested there were no changes...

$ python manage.py db migrate

> INFO  [alembic.runtime.migration] Context impl PostgresqlImpl. 

> INFO  [alembic.runtime.migration] Will assume transactional DDL. 

> INFO  [alembic.env] No changes in schema detected.

models.py file

from sqlalchemy.dialects.postgresql import JSON
from app import db


class Result(db.Model):
    __tablename__ = 'results'

    id = db.Column(db.Integer, primary_key=True)
    url = db.Column(db.String())
    datetime = db.Column(db.String())
    results = db.Column(JSON)
    errors = db.Column(JSON)


    def __init__(self, url, datetime, results, errors=None):
        self.url = url
        self.datetime = datetime
        self.results = results
        self.errors = errors


    def __repr__(self):
        return f"<id {self.id}>"

manage.py file

import os
import json

from flask_script import Manager
from flask_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()

1 Answers1

0

This answer from @dina-taklit worked for me...answer to "Flask-Migrate No Changes Detected to Schema on first migration"

1.(from Python REPL) Import the migrate model:

from flask_migrate import Migrate

2.Import the app, db objects:

from app import app, db

3.Initiate Migrate class:

migrate = Migrate(app, db)

4.Run the following:

db.create_all()

5.(from psql CLI) Drop the database

DROP DATABASE db_name;

6.Create it again =>

CREATE DATABSE db_name OWNER owner_name;

7.(from Ubuntu CLI)Point env var to your flask app file =>

export FLASK_APP=name_app.py

8.Run migration

python manage.py db migrate