7

Have spent literally the past three days trying to figure this out and just can't seem to get it. Have reviewed other posts here and elsewhere regarding the same issue (see here, here, here, among others) countless times and am apparently just too dense to figure it out myself. So here we are, any help would be greatly appreciated.

Project structure:

/project-path/
  /app.py
  /config.py
  /.flaskenv
  /app/__init__.py
  /app/models.py
  /app/routes.py

config.py

import os
from dotenv import load_dotenv

basedir = os.path.abspath(os.path.dirname(__file__))
load_dotenv(os.path.join(basedir, '.env'))

class Config(object):
    SECRET_KEY = os.environ.get('SECRET_KEY') or 'bleh'
    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or 'sqlite:///' + os.path.join(basedir, 'app.db')
    SQLALCHEMY_TRACK_MODIFICATIONS = False

app.py

from app import create_app
app = create_app() 

.flaskenv

FLASK_APP=app.py

/app/init.py

import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_marshmallow import Marshmallow

from config import Config

db = SQLAlchemy()
migrate = Migrate()
ma = Marshmallow()

def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)

    from app.models import Borrower

    db.init_app(app)
    migrate.init_app(app, db)

    from app.routes import borrowers_api
    app.register_blueprint(borrowers_api)

    return app

/app/models.py

from app import db, ma
from marshmallow_sqlalchemy import SQLAlchemyAutoSchema

class Borrower(db.Model):
    __tablename__ = 'borrowers'
    
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    borrower_name = db.Column(db.String(100), nullable=False, unique=True, index=True)
    
    def __init__(self, borrower_name):
        self.borrower_name = borrower_name

class BorrowerSchema(ma.SQLAlchemyAutoSchema):
    class Meta:
        model = Borrower
        include_relationships = True
        load_instance = True #Optional: deserialize to model instances  

borrower_schema = BorrowerSchema()
borrowers_schema = BorrowerSchema(many=True)

/app/routes.py

from flask import Blueprint, request, jsonify

from app.models import Borrower, borrower_schema, borrowers_schema
from app import db

borrowers_api = Blueprint('borrowers_api', __name__)

# Add a borrower
@borrowers_api.route('/borrower', methods=['POST'])
def add_borrower():
    borrower_name = request.get_json(force=True)['borrower_name']
    new_borrower = Borrower(borrower_name)
    db.session.add(new_borrower)
    db.session.commit()
    return borrower_schema.jsonify(new_borrower)

# Get single borrower by id
@borrowers_api.route('/borrower/<id>', methods=['GET'])
def get_borrower(id):
    borrower = Borrower.query.get(id)
    return borrower_schema.jsonify(borrower)

Yet all I continue to run into the following:

(env-name) C:\Users\...\flask-scratchwork\flask-restapi-tryagain>flask db init
Creating directory C:\Users\...\flask-scratchwork\flask-restapi-tryagain\migrations ...  done
Creating directory C:\Users\...\flask-scratchwork\flask-restapi-tryagain\migrations\versions ...  done
Generating C:\Users\...\flask-scratchwork\flask-restapi-tryagain\migrations\alembic.ini ...  done
Generating C:\Users\...\flask-scratchwork\flask-restapi-tryagain\migrations\env.py ...  done
Generating C:\Users\...\flask-scratchwork\flask-restapi-tryagain\migrations\README ...  done
Generating C:\Users\...\flask-scratchwork\flask-restapi-tryagain\migrations\script.py.mako ...  done
Please edit configuration/connection/logging settings in 'C:\\Users\\...\\flask-scratchwork\\flask-restapi-tryagain\\migrations\\alembic.ini' before proceeding.

Where am I going wrong?

lphhimself
  • 362
  • 2
  • 8

1 Answers1

16

Nevermind, think the answer was actually just proceed with flask db migrate despite:

Please edit configuration/connection/logging settings in 'C:\\Users\\...\\flask-scratchwork\\flask-restapi-tryagain\\migrations\\alembic.ini' before proceeding.

So for anyone else spending hours trying to discern precisely what type of edits flask-migrate wants you to make to to the "configuration/connection/logging settings" in alembic.ini...the answer is seemingly to just proceed with flask db migrate

lphhimself
  • 362
  • 2
  • 8
  • 8
    This message comes from Alembic, not from Flask-Migrate. The idea is that you should inspect the config file and adjust the settings to your liking. Flask-Migrate provides a starter configuration that should work for most projects out of the box, so ignoring this should be fine if you don't have any custom needs. – Miguel Grinberg May 24 '21 at 23:04