【问题标题】:AttributeError: 'ParseResult' object has no attribute 'drivername'AttributeError:“ParseResult”对象没有属性“drivername”
【发布时间】:2016-03-31 11:39:32
【问题描述】:

在 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,但这些答案没有帮助。

【问题讨论】:

    标签: postgresql heroku flask sqlalchemy flask-sqlalchemy


    【解决方案1】:

    尝试将import urlparse 更改为:

    from urlparse import urlparse
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-03
      • 1970-01-01
      • 2012-12-01
      • 2021-04-19
      • 2021-11-22
      • 1970-01-01
      • 1970-01-01
      • 2018-08-28
      相关资源
      最近更新 更多