0

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
)
Ryan
  • 259
  • 3
  • 10
  • 1
    Why is the `select` statement executed at all? My guess is that you might be executing some code during `import` on the module loads, which causes the issue. – van Oct 04 '22 at 07:10

1 Answers1

0

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.

Andrew Clark
  • 850
  • 7
  • 13
  • I added my __init__.py to the message above. – Ryan Oct 04 '22 at 20:01
  • I found it so I'm going to take this answer, but it was the last line of my init. Commenting that out let me continue. It looks like that was causing the problem on import in the other file, though, I'm not sure why it started doing that when it never did before. Must be some call I made elsewhere. – Ryan Oct 17 '22 at 16:23