我正在使用Python和Javascript进行CS50的Web编程,在Lecture4中,尝试创建postgresql数据库表时遇到以下错误:
.container {
display: flex;
margin: auto;
justify-content: flex-start;
flex-wrap: wrap;
}
.container .box{
width: 150px;
height: 150px;
box-sizing: border-box;
background: #ccc;
margin: 5px;
display: flex;
align-items: flex-end;
justify-content: center;
border: 2px solid #000;
}
我使用的代码在两个python文件中: 第一个称为models.py:
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'
第二个文件称为create.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)
你能帮我吗?!
答案 0 :(得分:1)
检查第os.getenv("postgresql://postgres:password@localhost/database1")
行
它不包含环境变量的名称。
参考:https://docs.python.org/3/library/os.html?highlight=getenv#os.getenv
样品用量:
$ python
Python 3.7.0 (default, Aug 20 2018, 15:06:39)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> editor = os.getenv('EDITOR')
>>> print(editor)
vi
答案 1 :(得分:1)
为阐明gbajson的答案,os.getenv
从特定的环境变量获取一个值。您 都需要将数据库URI存储在一个env var中(在启动Flask之前),然后从那里获取它:
app.config["SQLALCHEMY_DATABASE_URI"] = os.getenv("DATABASE_URI")
或者,不使用getenv将其直接硬编码为字符串:
app.config["SQLALCHEMY_DATABASE_URI"] = "postgresql://postgres:password@localhost/database1"
答案 2 :(得分:1)
我认为这与您尝试连接到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"
的系统上获取一个环境变量,并且您肯定没有使用该名称设置环境变量。这就是为什么为您的NoneType
驱动程序遇到postgres
错误:
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')
希望这很有道理!
答案 3 :(得分:0)
我正在创建基于Flask的Web应用程序博客,该博客通过SQLAlchemy模块连接到数据库。
在服务器上运行应用程序时,我得到了AttributeError: 'NoneType' object has no attribute 'drivername'
,这把我带到了这里。
以前(本地在开发中)我拥有的环境属性,现在有config.json
文件用于生产。
要访问它,我使用:
with open('path/to/config.json') as config_file:
,然后使用config = json.load(config_file)
将配置保存为python字典。
我完全像这样命名我的sql变量:
SQLALCHEMY_DATABASE_URI = config.get('SQLALCHEMY_DATABASE_URI')
我在它的config.json文件中使用了完全相同的名称。
我一直在寻找在虚拟环境中安装软件包的问题,但这没有帮助。问题是我打错了字,在config.json中调用了我的json密钥“ SQALCHEMY_DATABASE_URI”,但缺少“ L”。这种愚蠢的错别字使我犯了完全相同的错误,这使我花了太多时间,因此,如果有人正在寻找类似的解决方案,请首先仔细检查您的语法/拼写。
干杯!