【问题标题】:Flask-SQLAlchemy PostgreSQL get AttributeError: 'NoneType' object has no attribute 'drivername'Flask-SQLAlchemy PostgreSQL 获取 AttributeError: 'NoneType' 对象没有属性 'drivername'
【发布时间】:2021-05-03 15:18:19
【问题描述】:

尝试从 Flask 应用程序连接到 localhost 上现有的空 PostgreSQL 数据库时,我收到“AttributeError: 'NoneType' object has no attribute 'drivername'”。

这里是 .env:

...
SQLALCHEMY_DATABASE_URI='postgresql+psycopg2://postgres:PASSWORD@localhost/DATABASE' 
...

这是配置:

import os

basedir = os.path.abspath(os.path.dirname(__file__))

class Config(object):
    DEBUG = False
    TESTING = False
    SECRET_KEY = os.environ.get('SECRET_KEY')
    SQLALCHEMY_DATABASE_URI = os.environ.get('SQLALCHEMY_DATABASE_URI')
    SQLALCHEMY_TRACK_MODIFICATIONS = False

class ProdConfig(Config):
    DEBUG = False

class StagingConfig(Config):
    DEVELOPMENT = True
    DEBUG = True

class DevConfig(Config):
    DEVELOPMENT = True
    DEBUG = True

class TestConfig(Config):
    TESTING = True

这里是 app.extensions:

from flask_sqlalchemy import SQLAlchemy

db=SQLAlchemy()
...

这里是__init__:

from flask import Flask, Blueprint...
from . config import Config
from flask_login import LoginManager
from flask_migrate import Migrate
from pathlib import Path

login_manager = LoginManager()
login_manager.login_view = 'auth.login'

def create_app():
    app = Flask(__name__,
                static_url_path='',
                static_folder='../app/static',
                template_folder='../app/templates')

    app.config.from_object(Config)

    from .extensions import db
    db.init_app(app)

    Migrate(app, db)
    
    from .templates.auth import auth_bp
    from .views.main import main_bp

    app.register_blueprint(main_bp)
    app.register_blueprint(auth_bp, url_prefix='/auth')

    login_manager.init_app(app)

    return app

我检查了以下 Stackoverflow 答案:

db.create_all() 'NoneType' object has no attribute 'drivername',

SQLAlchemy: 'NoneType' object has no attribute 'drivername',

SQLAlchemy cannot connect to Postgresql on localhost,

Flask app gives a " 'NoneType' object has no attribute 'drivername' " error

它们似乎都指向 SQLALCHEMY_DATABASE_URI 声明中的错误。

这并不能解决我的问题。我试过了

>>> from app.extensions import db
>>> db.create_all()

和

>>> from app import create_app 
>>> app = create_app()
>>> from app.extensions import db
>>> db.create_all()

和

>>> from app import create_app
>>> app=create_app()
>>> app.app_context().push() 
>>> from app.extensions import db
>>> db.create_all()

结果是一样的。

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\...\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 1039, in create_all
    self._execute_for_all_tables(app, bind, 'create_all')
  File "C:\Users\...\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 1031, in _execute_for_all_tables        
    op(bind=self.get_engine(app, bind), **extra)
  File "C:\Users\...\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 962, in get_engine
    return connector.get_engine()
  File "C:\Users\...\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 555, in get_engine
    options = self.get_options(sa_url, echo)
  File "C:\Users\...\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 570, in get_options
    self._sa.apply_driver_hacks(self._app, sa_url, options)
  File "C:\Users\...\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 883, in apply_driver_hacks
    if sa_url.drivername.startswith('mysql'):
AttributeError: 'NoneType' object has no attribute 'drivername'

任何帮助将不胜感激。

【问题讨论】:

  • 我会检查环境变量 SQLALCHEMY_DATABASE_URI 是否已被设置,例如通过打印/记录其值。
  • 是否安装了python-dotenv?否则Flask 不支持.env 文件。
  • 你能试试from dotenv import load_dotenv然后load_dotenv()然后os.getenv('SQLALCHEMY_DATABASE_URI')吗?
  • &gt;&gt;&gt; load_dotenv() Python-dotenv could not parse statement starting at line 2 True &gt;&gt;&gt;
  • 您能否添加完整的.env 文件。错误在那个文件的某个地方!请确保删除敏感数据但保留结构和名称!

标签: python postgresql flask sqlalchemy flask-sqlalchemy


【解决方案1】:

由@rfkortekaas 解决:

>>> from dotenv import load_dotenv
>>> load_dotenv()
>>> import os
>>> os.getenv('SQLALCHEMY_DATABASE_URI')
>>> from app import create_app
>>> app = create_app()
>>> app.app_context().push()   
>>> from app.extensions import db
>>> db.create_all()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-31
    • 1970-01-01
    • 2014-12-29
    • 2020-06-25
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多