Heroku Postgres 上的 Flask-Migrate 因 ProgrammingError 而失败

Flask-Migrate on Heroku Postgres fails with ProgrammingError

我在更新 Heroku Postgres 上的模式时遇到了一些问题。我已经在 Heroku Dyno 上部署了一个工作应用程序,之前有一个 Postgres 数据库。我对数据库模式进行了一些更改(添加了新列)并将迁移更改提交到本地迁移文件夹。在我的本地主机上,升级后的数据库工作正常。但是,在将应用程序部署到 Heroku 时,出现 sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedColumn) column <my_col> does not exist 错误。

我试过以下命令
heroku run -a mydyno flask db history 它显示了正确的迁移历史 heroku run -a mydyno flask db migrate 并显示以下内容 -

INFO  [alembic.autogenerate.compare] Detected added table 'phishing_email'
INFO  [alembic.autogenerate.compare] Detected added index 'ix_phishing_email_created_at' on '['created_at']'
INFO  [alembic.autogenerate.compare] Detected added index 'ix_phishing_email_receiver_address' on '['receiver_address']'
INFO  [alembic.autogenerate.compare] Detected added index 'ix_phishing_email_sender_address' on '['sender_address']'
INFO  [alembic.ddl.postgresql] Detected sequence named 'user_user_id_seq' as owned by integer column 'user(user_id)', assuming SERIAL and omitting                                                                                             
INFO  [alembic.ddl.postgresql] Detected sequence named 'email_address_email_id_seq' as owned by integer column 'email_address(email_id)', assuming SERIAL and omitting                                                                          
INFO  [alembic.autogenerate.compare] Detected added column 'email_address.active'                                       
INFO  [alembic.autogenerate.compare] Detected added column 'email_address.email_password'                               
INFO  [alembic.autogenerate.compare] Detected added column 'email_address.last_mailbox_size'                            
INFO  [alembic.autogenerate.compare] Detected added column 'email_address.last_updated'                                 
INFO  [alembic.autogenerate.compare] Detected added column 'email_address.phishing_mail_detected'                       
INFO  [alembic.autogenerate.compare] Detected added column 'user.is_active'                                             
INFO  [alembic.autogenerate.compare] Detected added column 'user.is_admin'                                              
INFO  [alembic.autogenerate.compare] Detected added column 'user.last_logged_in'                                          Generating /app/migrations/versions/4e74c3c15c25_.py ...  done                        

迁移过程似乎检测到新添加的表和列,当我执行 heroku run -a mydyno flask db upgrade 之后日志显示

INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.                                                          
INFO  [alembic.runtime.migration] Will assume transactional DDL.

所以看起来一切顺利,但是在 heroku -a mydyno logs --tail 中部署我的应用程序时,日志显示错误 -

2020-11-13T15:21:01.291956+00:00 app[web.1]: sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedColumn) column user.last_logged_in does not exist                                                                                       
2020-11-13T15:21:01.291956+00:00 app[web.1]: LINE 1: ...r_password, "user".created_at AS user_created_at, "user".las... 2020-11-13T15:21:01.291956+00:00 app[web.1]: ^                                                                          
2020-11-13T15:21:01.291957+00:00 app[web.1]:                                                                            
2020-11-13T15:21:01.291958+00:00 app[web.1]: [SQL: SELECT "user".user_id AS user_user_id, "user".username AS user_username, "user".password AS user_password, "user".created_at AS user_created_at, "user".last_logged_in AS user_last_logged_in, "user".is_admin AS user_is_admin, "user".is_active AS user_is_active                                                  
2020-11-13T15:21:01.291958+00:00 app[web.1]: FROM "user"]

表示该列不存在。我做错了什么吗?或者是否有将本地数据库更改升级到 Heroku Postgres 的适当程序?我已确保 TRUNCATE CASCADE Heroku Postgres 上的所有表。

应用程序数据库在此部署之前一直在工作,但是只是为了使它更清楚,我的 Flask Config 对象包含 SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL'),它在我的 Heroku 应用程序的 Config Var 中设置为环境变量。

任何帮助将不胜感激,谢谢!

编辑:运行 heroku run -a mydno flask db history 显示所有迁移都存在并且 heroku run -a mydyno flask db current 显示正确的修订号,但架构仍未更新。

我最终使用 CLI 删除了所有表并使用从我的本地数据库导入了一个转储文件 pg_dump -h [host] -U [user] [db] > ./dump.out 导出我的本地数据库

在转储文件中将所有用户更改为我的 Heroku 数据库用户

psql -h [heroku-host] -U [postgres-user] [db-name] < <dump.file>

我很确定这不是正确的方法(因为它也会下降 alembic_version),但这对我有用。

我遇到了类似的问题。我将 sslmode=require 添加到我的 DATABASE_URL

# config.py

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

class Config(object):
    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL?sslmode=require') or \
        'sqlite:///' + os.path.join(basedir, 'app.db')
``