db.create_all() 'NoneType' 对象没有属性 'drivername' [英] db.create_all() 'NoneType' object has no attribute 'drivername'
问题描述
我正在使用 Python 和 Javascript 关注 CS50 的 Web 编程,在 Lecture4 中,我在尝试创建 postgresql 数据库表时遇到以下错误:
回溯(最近一次调用最后一次):文件create.py",第 19 行,在 <module> 中主要的()文件create.py",第 15 行,在主目录中db.create_all()文件/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py",第963行,在create_all中self._execute_for_all_tables(app, bind, 'create_all')文件/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py",第 955 行,在 _execute_for_all_tables操作(绑定=self.get_engine(应用程序,绑定),**额外)文件/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py",第896行,在get_engine返回connector.get_engine()文件/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py",第556行,在get_engineself._sa.apply_driver_hacks(self._app,信息,选项)文件/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py",第830行,在apply_driver_hacks如果 info.drivername.startswith('mysql'):AttributeError: 'NoneType' 对象没有属性 'drivername'
我使用的代码在两个 python 文件中:第一个叫做models.py:
from flask_sqlalchemy import SQLAlchemydb = SQLAlchemy()班级飞行(db.Model):__tablename__ = "航班"id = db.Column(db.Integer, primary_key=True)origin = db.Column(db.String, nullable=False)目标 = db.Column(db.String, nullable=False)持续时间 = db.Column(db.Integer, nullable=False)类乘客(db.Model):__tablename__ = "乘客"id = db.Column(db.Integer, primary_key=True)名称 = db.Column(db.String, nullable=False)Flight_id = db.Column(db.Integer, db.ForeignKey("flight.id"), nullable=False)
第二个文件叫做create.py:
导入操作系统从烧瓶导入烧瓶,render_template,请求从模型导入 *app = Flask(__name__)app.config["SQLALCHEMY_DATABASE_URI"] = os.getenv("postgresql://postgres:password@localhost/database1")app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = Falsedb.init_app(app)定义主():db.create_all()如果 __name__ == "__main__":使用 app.app_context():主要的()
你能帮我吗?!
我认为这是您尝试连接到 Postgres
数据库的方式的问题:
app.config["SQLALCHEMY_DATABASE_URI"] = os.getenv("postgresql://postgres:password@localhost/database1")
您可能希望此行改为以下内容:
app.config["SQLALCHEMY_DATABASE_URI"] = "postgresql://postgres:password@localhost/database1"
因为 os.getenv(...)
当前正在尝试在您的系统上获取一个名为:"postgresql://postgres:password@localhost/database1"
并且您肯定没有使用此名称设置环境变量.这就是您的 postgres
驱动程序收到 NoneType
错误的原因:><块引用>
AttributeError: 'NoneType' 对象没有属性 'drivername'.
如果您想使用环境变量来获取数据库连接字符串,请在您的 .bash_profile
或 .bashrc
文件中执行如下操作:
export SQLALCHEMY_DATABASE_URI='postgresql://postgres:password@localhost/database1'
然后将您的数据库连接代码更改为以下内容:
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get('SQLALCHEMY_DATABASE_URI')
希望这是有道理的!
I am following the CS50's Web Programming with Python and Javascript and in Lecture4 I have had the following error trying to create a postgresql database table:
Traceback (most recent call last):
File "create.py", line 19, in <module>
main()
File "create.py", line 15, in main
db.create_all()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 963, in create_all
self._execute_for_all_tables(app, bind, 'create_all')
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 955, in _execute_for_all_tables
op(bind=self.get_engine(app, bind), **extra)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 896, in get_engine
return connector.get_engine()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 556, in get_engine
self._sa.apply_driver_hacks(self._app, info, options)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 830, in apply_driver_hacks
if info.drivername.startswith('mysql'):
AttributeError: 'NoneType' object has no attribute 'drivername'
The code that I have used is in two python file: The first 1 called models.py:
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class Flight(db.Model):
__tablename__ = "flights"
id = db.Column(db.Integer, primary_key=True)
origin = db.Column(db.String, nullable=False)
destination = db.Column(db.String, nullable=False)
duration = db.Column(db.Integer, nullable=False)
class Passenger(db.Model):
__tablename__ = "passengers"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, nullable=False)
flight_id = db.Column(db.Integer, db.ForeignKey("flight.id"), nullable=False)
The second file is called create.py:
import os
from flask import Flask, render_template, request
from models import *
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = os.getenv("postgresql://postgres:password@localhost/database1")
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.init_app(app)
def main():
db.create_all()
if __name__ == "__main__":
with app.app_context():
main()
Can you help me?!
I think this is an issue with how you're attempting to connect to your Postgres
database:
app.config["SQLALCHEMY_DATABASE_URI"] = os.getenv("postgresql://postgres:password@localhost/database1")
you probably want this line to be the following instead:
app.config["SQLALCHEMY_DATABASE_URI"] = "postgresql://postgres:password@localhost/database1"
since the os.getenv(...)
is currently trying to get an environment variable on your system named: "postgresql://postgres:password@localhost/database1"
and you surely didn't set up an environment variable with this name.. Which is why you're getting a NoneType
error for your postgres
driver:
AttributeError: 'NoneType' object has no attribute 'drivername'.
If you want to use an environment variable to get your database connection string, do something like the following in your .bash_profile
or .bashrc
file:
export SQLALCHEMY_DATABASE_URI='postgresql://postgres:password@localhost/database1'
then change your database connection code to the following:
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get('SQLALCHEMY_DATABASE_URI')
Hopefully that makes sense!
这篇关于db.create_all() 'NoneType' 对象没有属性 'drivername'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!