我正在阅读 flask/sqlalchemy 教程 https://pythonhosted.org/Flask-SQLAlchemy/quickstart.html#a-minimal-application 我将我的数据库 url 配置为: postgres://用户名:密码@localhost:5432/数据库名
当我在交互式 python shell 中运行 db.create_all() 时,它不会抛出任何错误,但它也不会执行任何操作。
据我了解,应该创建包含三列的用户表; ID、用户名和电子邮件。
from flask import Flask, url_for, render_template
from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgres://username:password@localhost:5432/dbname'
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
email = db.Column(db.String(120), unique=True)
def __init__(self, username, email):
self.username = username
self.email = email
def __repr__(self):
return '<User %r>' % self.username
app.debug = True
@app.route("/")
def hello():
return render_template('hello.html')
if __name__ == "__main__":
app.run()
最佳答案
应该是准确的格式。
app.config['SQLALCHEMY_DATABASE_URI'] = "postgresql://postgres:postgres@localhost/DBNAME"
其中postgres
为用户名,postgres
为密码,localhost
为地址
关于python - 我的本地 postgresql 数据库 url 的形式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24727902/