Why Can't I Add a Column using Alembic Because it isn't in the Table?

Question:

So I’m trying to use alembic to add a column to my table. Here’s my class defining the table with the new line in bold:

class Account(db.Model):
    id = db.Column(db.Integer, index=True, primary_key=True)
    account_number = db.Column(db.String(10), index=True)
    primary_member_fk = db.Column(db.Integer)
    first_deposit = db.Column(db.DateTime, index=True)
    is_business = db.Column(db.Boolean) #I'm adding this line

When I run:

flask db migrate

I get the following error that looks like it isn’t letting me add the new column because it doesn’t already exist in the database, which seems pretty circular.

Invalid column name 'is_business'. (207) (SQLExecDirectW)")
[SQL: SELECT account.id AS account_id, account.account_number AS account_account_number, account.primary_member_fk 
AS account_primary_member_fk, account.first_deposit AS account_first_deposit, account.is_business AS account_is_business
FROM account]

What is causing this error? And how do I correct it?

Here is my init.py file. I don’t see anything that causes a problem, but perhaps you can correct the error of my ways?

from flask import Flask
from flask_bootstrap import Bootstrap
from flask_ldap3_login import LDAP3LoginManager
from flask_login import LoginManager
from flask_migrate import Migrate
from flask_moment import Moment
from flask_sqlalchemy import SQLAlchemy
from ldap3 import Tls
import ssl
from config import Config
import logging

logging.basicConfig()
logging.getLogger('flask_ldap3_login').setLevel(logging.DEBUG)

app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app,db)
login = LoginManager(app)
ldap_manager = LDAP3LoginManager(app)
login.login_view = 'login'
bootstrap = Bootstrap(app)
moment = Moment(app)


tls_ctx = Tls(
    validate=ssl.CERT_REQUIRED,
    version=ssl.PROTOCOL_TLSv1,
    ca_certs_file='./certificate.cer',
    valid_names=[
        'ldaps.company.org',
    ]
)

ldap_manager.add_server(
    app.config.get('LDAP_HOST'),
    app.config.get('LDAP_PORT'),
    app.config.get('LDAP_USE_SSL'),
    tls_ctx=tls_ctx
)
Asked By: Ryan

||

Answers:

Can you show us the structure of your project? Assuming you have an __init__.py file in the root of your project, insure you have the following migrate line before any blueprint calls, etc.

from flask_migrate import Migrate
from flask import Flask

app = Flask(__name__)
migrate = Migrate(app, db)

Before running flask db migrate in your terminal. Make sure your project is not actively running in any other terminals. Also, navigate to the root of your project before making the flask db migrate call.

Answered By: Andrew Clark