AttributeError:'ParseResult'对象没有属性'drivername'

时间:2015-12-25 12:13:49

标签: postgresql heroku flask sqlalchemy flask-sqlalchemy

在Heroku上运行应用程序时遇到问题。本地版本似乎工作正常,但我无法在heroku平台上调用create_db()

这是错误跟踪。

(venv)S-MBP:LaunchPage3 S$ heroku run python
Running python on herokuapp... up, run.5609
Python 2.7.11 (default, Dec  7 2015, 21:16:24) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from app import db
>>> db.create_all()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/app/.heroku/python/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 972, in create_all
self._execute_for_all_tables(app, bind, 'create_all')
File "/app/.heroku/python/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 964, in _execute_for_all_tables
op(bind=self.get_engine(app, bind), **extra)
File "/app/.heroku/python/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 910, in get_engine
return connector.get_engine()
File "/app/.heroku/python/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 542, in get_engine
self._sa.apply_driver_hacks(self._app, info, options)
File "/app/.heroku/python/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 846, in apply_driver_hacks
if info.drivername.startswith('mysql'):
AttributeError: 'ParseResult' object has no attribute 'drivername'

这是app.py

import os, requests, json
from flask import Flask, render_template, request, redirect
import psycopg2, urlparse
from flask.ext.sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True

if 'PRODUCTION' in os.environ:
    urlparse.uses_netloc.append("postgres")
    app.config['SQLALCHEMY_DATABASE_URI'] = urlparse.urlparse(os.environ["DATABASE_URL"])
else:
    app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://postgres:root@localhost:5432/db'
    app.config['SQLALCHEMY_ECHO'] = True

db = SQLAlchemy(app)

[CODE - ONE FILE APPLICATION WITH DB MODELS ETC]

if __name__ == '__main__':
    db.create_all()
    port = int(os.environ.get('PORT',5000))

    if 'PRODUCTION' in os.environ:
        app.debug = False
        app.run(host='0.0.0.0', port=port)
    else:
        app.debug = True
        app.run(host='127.0.0.1', port=port)

任何指针都将受到赞赏。我在SO中尝试了other question,但这些答案没有帮助。

1 个答案:

答案 0 :(得分:0)

尝试将import urlparse更改为:

from urlparse import urlparse