最近在學(xué)flask ,我是跟《flask-web開發(fā):基于Python的Web應(yīng)用開發(fā)實(shí)戰(zhàn)》 這本書一步一步實(shí)現(xiàn)的,也就是在github上 flasky 這個(gè)項(xiàng)目。中間遇到一兩個(gè)比較大的問題,記錄下來*
flask + heroku +postagres
1. 問題一:
在成功 heroku服務(wù)器git后, 初始和更新數(shù)據(jù)庫的時(shí)候,如類似以下錯(cuò)誤:
heroku run python manager.py deploy # 初始和更新數(shù)據(jù)庫
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
大致是上面錯(cuò)誤,具體看下列錯(cuò)誤一**
一開始,看報(bào)錯(cuò)提示,以為是自己的
psycopg2出錯(cuò)了(我安裝二進(jìn)制包psycopg2-binary,因?yàn)榘惭b不上psycopg2)以為是數(shù)據(jù)庫 結(jié)構(gòu)有點(diǎn)問題,后面優(yōu)化數(shù)據(jù)結(jié)構(gòu)與類型,還是報(bào)錯(cuò)。
執(zhí)行
git push heroku master,可以運(yùn)行,打開APP的鏈接,只要是動(dòng)態(tài)連接到數(shù)據(jù)庫的頁面都會(huì)500Internal Server Error報(bào)服務(wù)器錯(cuò)。顯然是連接數(shù)據(jù)庫出錯(cuò)執(zhí)行
heroku logs -t發(fā)現(xiàn)會(huì)報(bào)類似sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedTable) relation "posts" does not exist這樣的錯(cuò),與之前一樣的錯(cuò)。本地也使用本地
postagres庫,一切正常。排除數(shù)據(jù)庫出錯(cuò),代碼問題。
無奈~~,想起之前在 stackoverflow搜索 Internal Server Error+flask+heroku就看到有人說是把migrations版本都刪掉,無奈,做個(gè)備份,把migrations文件夾刪掉,git 和初始化后,居然沒有報(bào)錯(cuò),ztyd神奇!
解決方法:
在確定,代碼沒問題,在本地的虛擬環(huán)境正常運(yùn)行可采取以下操作:
刪掉煩人的數(shù)據(jù)庫版本控制
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 ,記得給我留言哦~~~
* 錯(cuò)誤一
heroku run python manager.py deploy # 初始和更新數(shù)據(jù)庫
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