最近在学flask ,我是跟《flask-web开发:基于Python的Web应用开发实战》 这本书一步一步实现的,也就是在github
上 flasky 这个项目。中间遇到一两个比较大的问题,记录下来*
flask
+ heroku
+postagres
1. 问题一:
在成功 heroku
服务器git
后, 初始和更新数据库的时候,如类似以下错误:
heroku run python manager.py deploy # 初始和更新数据库
Running python manager.py deploy on ⬢ flasky-blog-mirui... ⣾
Running python manager.py deploy on ⬢ flasky-blog-mirui... ⣷ starting, run.7767 (Free)
Running python manager.py deploy on ⬢ flasky-blog-mirui... up, run.7767 (Free)
INFO [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO [alembic.runtime.migration] Will assume transactional DDL.
INFO [alembic.runtime.migration] Running upgrade -> 8d6dba4cf1a4, the second migration
psycopg2.errors.UndefinedTable: relation "users" does not exist
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedTable) relation "users" does not exist
大致是上面错误,具体看下列错误一**
-
一开始,看报错提示,以为是自己的
psycopg2
出错了(我安装二进制包psycopg2-binary
,因为安装不上psycopg2
) -
以为是数据库 结构有点问题,后面优化数据结构与类型,还是报错。
-
执行
git push heroku master
,可以运行,打开APP
的链接,只要是动态连接到数据库的页面都会500
Internal Server Error
报服务器错。显然是连接数据库出错 -
执行
heroku logs -t
发现会报类似sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedTable) relation "posts" does not exist
这样的错,与之前一样的错。 -
本地也使用本地
postagres
库,一切正常。排除数据库出错,代码问题。
无奈~~,想起之前在 stackoverflow
搜索 Internal Server Error
+flask
+heroku
就看到有人说是把migrations
版本都删掉,无奈,做个备份,把migrations
文件夹删掉,git
和初始化后,居然没有报错,ztyd神奇!
解决方法:
在确定,代码没问题,在本地的虚拟环境正常运行可采取以下操作:
删掉烦人的数据库版本控制
migrations
文件夹
rm -rf ./migrations
git add .
git commit -am '马上要好了'
git push heroku master
heroku run python manage,py deploy
好了,这就可以打开你的heroku app
了。
欢迎你们来我的 heroku app
flasky-blog-mirui ,记得给我留言哦~~~
* 错误一
heroku run python manager.py deploy # 初始和更新数据库
Running python manager.py deploy on ⬢ flasky-blog-mirui... ⣾
Running python manager.py deploy on ⬢ flasky-blog-mirui... ⣷ starting, run.7767 (Free)
Running python manager.py deploy on ⬢ flasky-blog-mirui... up, run.7767 (Free)
INFO [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO [alembic.runtime.migration] Will assume transactional DDL.
INFO [alembic.runtime.migration] Running upgrade -> 8d6dba4cf1a4, the second migration
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1244, in _execute_context
cursor, statement, parameters, context
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 552, in do_execute
cursor.execute(statement, parameters)
psycopg2.errors.UndefinedTable: relation "users" does not exist
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manager.py", line 83, in <module>
manager.run()
File "/app/.heroku/python/lib/python3.6/site-packages/flask_script/__init__.py", line 417, in run
result = self.handle(argv[0], argv[1:])
File "/app/.heroku/python/lib/python3.6/site-packages/flask_script/__init__.py", line 386, in handle
res = handle(*args, **config)
File "/app/.heroku/python/lib/python3.6/site-packages/flask_script/commands.py", line 216, in __call__
return self.run(*args, **kwargs)
File "manager.py", line 67, in deploy
upgrade()
File "/app/.heroku/python/lib/python3.6/site-packages/flask_migrate/__init__.py", line 95, in wrapped
f(*args, **kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/flask_migrate/__init__.py", line 280, in upgrade
command.upgrade(config, revision, sql=sql, tag=tag)
File "/app/.heroku/python/lib/python3.6/site-packages/alembic/command.py", line 276, in upgrade
script.run_env()
File "/app/.heroku/python/lib/python3.6/site-packages/alembic/script/base.py", line 475, in run_env
util.load_python_file(self.dir, "env.py")
File "/app/.heroku/python/lib/python3.6/site-packages/alembic/util/pyfiles.py", line 90, in load_python_file
module = load_module_py(module_id, path)
File "/app/.heroku/python/lib/python3.6/site-packages/alembic/util/compat.py", line 177, in load_module_py
spec.loader.exec_module(module)
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "migrations/env.py", line 96, in <module>
run_migrations_online()
File "migrations/env.py", line 90, in run_migrations_online
context.run_migrations()
File "<string>", line 8, in run_migrations
File "/app/.heroku/python/lib/python3.6/site-packages/alembic/runtime/environment.py", line 839, in run_migrations
self.get_context().run_migrations(**kw)
File "/app/.heroku/python/lib/python3.6/site-packages/alembic/runtime/migration.py", line 362, in run_migrations
step.migration_fn(**kw)
File "/app/migrations/versions/8d6dba4cf1a4_the_second_migration.py", line 21, in upgrade
op.add_column('users', sa.Column('confirmed', sa.Boolean(), nullable=True))
File "<string>", line 8, in add_column
File "<string>", line 3, in add_column
File "/app/.heroku/python/lib/python3.6/site-packages/alembic/operations/ops.py", line 1904, in add_column
return operations.invoke(op)
File "/app/.heroku/python/lib/python3.6/site-packages/alembic/operations/base.py", line 345, in invoke
return fn(self, operation)
File "/app/.heroku/python/lib/python3.6/site-packages/alembic/operations/toimpl.py", line 131, in add_column
operations.impl.add_column(table_name, column, schema=schema)
File "/app/.heroku/python/lib/python3.6/site-packages/alembic/ddl/impl.py", line 231, in add_column
self._exec(base.AddColumn(table_name, column, schema=schema))
File "/app/.heroku/python/lib/python3.6/site-packages/alembic/ddl/impl.py", line 134, in _exec
return conn.execute(construct, *multiparams, **params)
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 988, in execute
return meth(self, multiparams, params)
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/sql/ddl.py", line 72, in _execute_on_connection
return connection._execute_ddl(self, multiparams, params)
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1050, in _execute_ddl
compiled,
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1248, in _execute_context
e, statement, parameters, cursor, context
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1466, in _handle_dbapi_exception
util.raise_from_cause(sqlalchemy_exception, exc_info)
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 398, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 152, in reraise
raise value.with_traceback(tb)
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1244, in _execute_context
cursor, statement, parameters, context
File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 552, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedTable) relation "users" does not exist
网友评论