2013-10-30 72 views
1

我確定這是一個很容易修復的錯誤,如果我只能找到它的位置。這是從燒瓶中的應用程序錯誤:SQLAlchemy無法連接到本地主機上的Postgresql

11:58:18 web.1 | ERROR:xxxxxx.core:Exception on/[GET] 
11:58:18 web.1 | Traceback (most recent call last): 
11:58:18 web.1 | File "/Library/Python/2.7/site-packages/flask/app.py", line 1817, in wsgi_app 
11:58:18 web.1 |  response = self.full_dispatch_request() 
11:58:18 web.1 | File "/Library/Python/2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request 
11:58:18 web.1 |  rv = self.handle_user_exception(e) 
11:58:18 web.1 | File "/Library/Python/2.7/site-packages/flask/app.py", line 1381, in handle_user_exception 
11:58:18 web.1 |  reraise(exc_type, exc_value, tb) 
11:58:18 web.1 | File "/Library/Python/2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request 
11:58:18 web.1 |  rv = self.dispatch_request() 
11:58:18 web.1 | File "/Library/Python/2.7/site-packages/flask/app.py", line 1461, in dispatch_request 
11:58:18 web.1 |  return self.view_functions[rule.endpoint](**req.view_args) 
11:58:18 web.1 | File "xxxxxxx/web.py", line 202, in home 
11:58:18 web.1 |  d = {'featured': cached_apps.get_featured_front_page(), 
11:58:18 web.1 | File "/Library/Python/2.7/site-packages/flask_cache/__init__.py", line 245, in decorated_function 
11:58:18 web.1 |  rv = f(*args, **kwargs) 
11:58:18 web.1 | File "/Users/xxxxxxx/Desktop/PythonProjects/xxxxxx/xxxxxx2/xxxxxxx/cache/apps.py", line 35, in get_featured_front_page 
11:58:18 web.1 |  results = db.engine.execute(sql) 
11:58:18 web.1 | File "/Library/Python/2.7/site-packages/flask_sqlalchemy/__init__.py", line 780, in engine 
11:58:18 web.1 |  return self.get_engine(self.get_app()) 
11:58:18 web.1 | File "/Library/Python/2.7/site-packages/flask_sqlalchemy/__init__.py", line 797, in get_engine 
11:58:18 web.1 |  return connector.get_engine() 
11:58:18 web.1 | File "/Library/Python/2.7/site-packages/flask_sqlalchemy/__init__.py", line 470, in get_engine 
11:58:18 web.1 |  self._sa.apply_driver_hacks(self._app, info, options) 
11:58:18 web.1 | File "/Library/Python/2.7/site-packages/flask_sqlalchemy/__init__.py", line 739, in apply_driver_hacks 
11:58:18 web.1 |  if info.drivername.startswith('mysql'): 
11:58:18 web.1 | AttributeError: 'NoneType' object has no attribute 'drivername' 

從我已經能夠在網上找到的,好像問題是,我可能沒有正確連接到數據庫。該應用程序在Heroku中運行良好,但不是在本地主機上運行時。

which psql

/Applications/Postgres.app/Contents/MacOS/bin/psql 

which postgres

/Applications/Postgres.app/Contents/MacOS/bin/postgres 

Postgres.app運行在5432

我不知道要檢查什麼。

如果它應該連接到heroku上的同一個postgres數據庫不管,爲什麼它會在heroku上工作,而不是從localhost上工作?

也許localhost上的應用程序使用的是Postgres的錯誤版本?我試着卸載它們(只留下Postgres.app),但我不確定是否在我的計算機上還有任何可能導致衝突的事情。我將如何檢查?我會很感激任何幫助。

編輯:從alembic.ini文件更多信息
段:

[alembic] 
# path to migration scripts 
script_location = alembic 

# template used to generate migration files 
# file_template = %%(rev)s_%%(slug)s 

# under Heroku, the line below needs to be inferred from 
# the environment 
sqlalchemy.url = postgres://xxxxxxxxxx:[email protected]:5432/xxxxxxxxx 

# Logging configuration 
[loggers] 
keys = root,sqlalchemy,alembic 

我有一個產生同樣的錯誤很短的腳本: 我對

#!/usr/bin/env python 
import os 
import sys 
import optparse 
import inspect 

import xxxxxxx.model as model 
from xxxxxx.core import db 
import xxxxx.web as web 

from alembic.config import Config 
from alembic import command  
def setup_alembic_config(): 
    if "DATABASE_URL" not in os.environ: 
     alembic_cfg = Config("alembic.ini") 
    else: 
     dynamic_filename = "alembic-heroku.ini" 
     with file("alembic.ini.template") as f: 
      with file(dynamic_filename, "w") as conf: 
       for line in f.readlines(): 
        if line.startswith("sqlalchemy.url"): 
         conf.write("sqlalchemy.url = %s\n" % 
            os.environ['DATABASE_URL']) 
        else: 
         conf.write(line) 
     alembic_cfg = Config(dynamic_filename) 

    command.stamp(alembic_cfg, "head") 

def db_create(): 
    '''Create the db''' 
    db.create_all() 
    # then, load the Alembic configuration and generate the 
    # version table, "stamping" it with the most recent rev: 
    setup_alembic_config() 
    # finally, add a minimum set of categories: Volunteer Thinking, Volunteer Sensing, Published and Draft 
    categories = [] 
    categories.append(model.Category(name="Thinking", 
            short_name='thinking', 
            description='Volunteer Thinking apps')) 
    categories.append(model.Category(name="Volunteer Sensing", 
            short_name='sensing', 
            description='Volunteer Sensing apps')) 
    db.session.add_all(categories) 
    db.session.commit() 
運行 python cli.py db_create 我得到:

Traceback (most recent call last): 
    File "cli.py", line 111, in <module> 
    _main(locals()) 
    File "cli.py", line 106, in _main 
    _methods[method](*args[1:]) 
    File "cli.py", line 33, in db_create 
    db.create_all() 
    File "/Library/Python/2.7/site-packages/flask_sqlalchemy/__init__.py", line 856, in create_all 
    self._execute_for_all_tables(app, bind, 'create_all') 
    File "/Library/Python/2.7/site-packages/flask_sqlalchemy/__init__.py", line 848, in _execute_for_all_tables 
    op(bind=self.get_engine(app, bind), tables=tables) 
    File "/Library/Python/2.7/site-packages/flask_sqlalchemy/__init__.py", line 797, in get_engine 
    return connector.get_engine() 
    File "/Library/Python/2.7/site-packages/flask_sqlalchemy/__init__.py", line 470, in get_engine 
    self._sa.apply_driver_hacks(self._app, info, options) 
    File "/Library/Python/2.7/site-packages/flask_sqlalchemy/__init__.py", line 739, in apply_driver_hacks 
    if info.drivername.startswith('mysql'): 
AttributeError: 'NoneType' object has no attribute 'drivername' 
+0

你可以編輯你的問題,幷包括你的Flask配置(顯然,審查任何敏感信息)。 –

回答

5

我的猜測是你沒有正確configured Flask-SQLAlchemy。你有很多代碼似乎試圖配置它,但沒有經過全部的代碼,我的猜測是它要麼不正確地設置你的配置,要麼設置你的配置太遲。

確保在調用任何會觸及數據庫的任何內容(如db.create_all())之前,請確保您的app.config["SQLALCHEMY_DATABASE_URI"]設置爲正確的URI。它可能設置爲None,這是造成您的問題。

+0

你是對的。我發現'SQLALCHEMY_DATABASE_URI'被設置爲'os.environ.get('DATABASE_URL')',這在任何地方都不存在。用URL創建一個.env文件。非常感謝! – luisdaniel