首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SQLAlchemy无法连接本地主机上的Postgresql

SQLAlchemy无法连接本地主机上的Postgresql
EN

Stack Overflow用户
提问于 2013-10-30 17:41:26
回答 2查看 5.6K关注 0票数 3

我肯定这是一个很容易修复的错误,如果我能找到它在哪里。这是来自Flask应用程序的错误:

代码语言:javascript
复制
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

代码语言:javascript
复制
/Applications/Postgres.app/Contents/MacOS/bin/psql

which postgres

代码语言:javascript
复制
/Applications/Postgres.app/Contents/MacOS/bin/postgres

Postgres.app正在5432上运行。

我不知道还能查些什么。

如果它应该连接到heroku上的同一个postgres DB,那么为什么它要在heroku上工作,而不是从本地主机运行呢?

也许本地主机上的应用程序使用了错误的Postgres版本?我试着卸载它们(只离开Postgres.app),但我不确定我的计算机上是否还有引起冲突的东西。我该怎么查呢?我很感谢你的帮助。

编辑:更多信息

从alembic.ini文件中提取的段:

代码语言:javascript
复制
[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:xxxxxxxxxxxx@xxxxxx.compute-1.amazonaws.com:5432/xxxxxxxxx

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

我有一个短脚本,它会产生同样的错误:我在python cli.py db_create上运行

代码语言:javascript
复制
#!/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()

我得到了:

代码语言:javascript
复制
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'
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-10-30 18:25:06

我猜你没有正确的配置瓶-SQLAlchemy。您有很多代码似乎都在尝试配置它,但如果没有全部完成,我的猜测是,它要么设置错了配置,要么设置得太晚。

在调用将访问数据库(如db.create_all())的任何内容之前,请确保将app.config["SQLALCHEMY_DATABASE_URI"]设置为正确的URI。它可能设置为None,这导致了您的问题。

票数 9
EN

Stack Overflow用户

发布于 2020-08-26 09:50:23

马克·希尔德雷思的回答是7岁,我花了两天时间才来到这里--我犯了这样的错误:

失败的测试/test_models.py s.py::test_终结点- AttributeError:'NoneType‘对象没有属性'drivername’

错误测试/test_models.py s.py::test_endpoint2 2- AttributeError:'NoneType‘对象没有属性'response_class’

回溯显示的感觉如下:

代码语言:javascript
复制
self = <[AttributeError("'NoneType' object has no attribute 'drivername'") raised in repr()] SQLAlchemy object at 0x1fb8a622848>, app = <Flask 'app'>, sa_url = None, options = {}

最后,对于tihs的回答,我记得,Flask有时需要设置配置变量。非常感谢-- os.environ.get('mysql://root:1234@127.0.0.1/kvadrat?charset=utf8') = app.config"SQLALCHEMY_DATABASE_URI“

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19690119

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档