sqlite3.OperationalError)在Heroku上没有这样的表

时间:2019-05-15 22:06:45

标签: python heroku flask sqlalchemy

我正在使用https://github.com/hack4impact/flask-base(烧瓶样板)作为项目的基础。该项目按预期在Windows本地运行。

我不需要在代码的生产版本上进行任何数据库更改,因此,为简单起见,我决定在本地和heroku上都使用sqllite。考虑到这一点,我将https://github.com/hack4impact/flask-base/blob/master/config.py中的生产类更改为:

class ProductionConfig(Config):
    # SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or 'sqlite:///' + os.path.join(basedir, 'data.sqlite')
    SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'data-dev.sqlite')
    print('IN PRODUCTION '+ SQLALCHEMY_DATABASE_URI)
    SSL_DISABLE = (os.environ.get('SSL_DISABLE') or 'True') == 'True'

当我部署代码并尝试登录时,我看到500错误。日志显示:

2019-05-15T21:47:28.749283+00:00 app[web.1]:     raise value.with_traceback(tb)
2019-05-15T21:47:28.749284+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
2019-05-15T21:47:28.749286+00:00 app[web.1]:     context)
2019-05-15T21:47:28.749287+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 470, in do_execute
2019-05-15T21:47:28.749289+00:00 app[web.1]:     cursor.execute(statement, parameters)
2019-05-15T21:47:28.749301+00:00 app[web.1]: sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: users [SQL: 'SELECT users.id AS users_id, users.confirmed AS users_confirmed, users.first_name AS users_first_name, users.last_name AS users_last_name, users.email AS users_email, users.password_hash AS users_password_hash, users.role_id AS users_role_id \nFROM users \nWHERE users.email = ?\n LIMIT ? OFFSET ?'] [parameters: ('me@test.com', 1, 0)]
Disconnected from log stream. There may be events happening that you do not see here! Attempting to reconnect...
2019-05-15T21:47:28.749277+00:00 app[web.1]:     exc_info
2019-05-15T21:47:28.749278+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
2019-05-15T21:47:28.749280+00:00 app[web.1]:     reraise(type(exception), exception, tb=exc_tb, cause=cause)
2019-05-15T21:47:28.749281+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 186, in reraise
2019-05-15T21:47:28.749283+00:00 app[web.1]:     raise value.with_traceback(tb)
2019-05-15T21:47:28.749284+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
2019-05-15T21:47:28.749286+00:00 app[web.1]:     context)
2019-05-15T21:47:28.749287+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 470, in do_execute
2019-05-15T21:47:28.749289+00:00 app[web.1]:     cursor.execute(statement, parameters)
2019-05-15T21:47:28.749301+00:00 app[web.1]: sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: users [SQL: 'SELECT users.id AS users_id, users.confirmed AS users_confirmed, users.first_name AS users_first_name, users.last_name AS users_last_name, users.email AS users_email, users.password_hash AS users_password_hash, users.role_id AS users_role_id \nFROM users \nWHERE users.email = ?\n LIMIT ? OFFSET ?'] [parameters: ('me@test.com', 1, 0)]

所以看来sqlalchemy找不到表。我想知道是否该行:

SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'data-dev.sqlite')

然后是访问平面文件db(data-dev.sqlite)的错误方法

1 个答案:

答案 0 :(得分:2)

You really shouldn't use SQLite on Heroku。其文件系统is ephemeral。您下次对dyno重新启动时,对文件所做的任何更改都将丢失。 happens frequently(每天至少一次)。

原始代码做了正确的事情:

SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or 'sqlite:///' + os.path.join(basedir, 'data.sqlite')

如果设置了DATABASE_URL环境变量,它将用于连接到数据库。由于psycopg2 is in your requirements.txt file Heroku应该automatically provision a PostgreSQL database并将DATABASE_URL变量设置为其连接字符串。

如果您确实必须使用SQLite(我强烈建议 反对),请将其视为只读。您必须在本地开发计算机上提交数据库文件,并将该提交推送到Heroku。即使那样,它也可能无法正常工作。 Heroku以generates errors if Ruby users even try to install the sqlite3 gem而著名。