- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在学习 CS50 的 Web Programming with Python and Javascript,在 Lecture4 中,我在尝试创建 postgresql 数据库表时遇到以下错误:
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'
我使用的代码在两个 python 文件中:第一个叫做 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)
第二个文件叫做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()
你能帮帮我吗?!
最佳答案
我认为这是您尝试连接到 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' object has no attribute 'drivername'.
如果您想使用环境变量来获取数据库连接字符串,请在您的 .bash_profile
或 .bashrc
文件中执行类似以下操作:
export SQLALCHEMY_DATABASE_URI='postgresql://postgres:password@localhost/database1'
然后将您的数据库连接代码更改为以下内容:
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get('SQLALCHEMY_DATABASE_URI')
希望这是有道理的!
关于python - db.create_all() 'NoneType' 对象没有属性 'drivername',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55667117/
我将 Flask 和 SQLAlchemy 集成到我的应用程序中,但可以理解为什么在登录页面获取大堆栈跟踪。从 Debian 切换到 Mac 操作系统后,我现在会遇到此错误。 virtuaelnv 中
我在 Heroku 上运行应用程序时遇到问题。本地版本似乎工作正常,但我无法在 heroku 平台上调用 create_db()。 这是错误轨迹。 (venv)S-MBP:LaunchPage3 S$
我正在学习 CS50 的 Web Programming with Python and Javascript,在 Lecture4 中,我在尝试创建 postgresql 数据库表时遇到以下错误:
当我启动我的应用程序时出现此错误。显然它与 SQLAlchemy 有一些关系。我一直在 Corey Schaffer Flask 教程的帮助下处理这个例子。 > File "C:\Users\Mate
我正在尝试使用 Flask-SqlAlchemy 连接到 MySQL 数据库,这是我的参数: SQLALCHEMY_DATABASE_URI = 'mysql://root:pass@127.0.0.
我是一名优秀的程序员,十分优秀!