我正在阅读一个烧瓶/ sqlalchemy教程 https://pythonhosted.org/Flask-SQLAlchemy/quickstart.html#a-minimal-application 并且我将我的数据库URL配置为: postgres的://用户名:密码@本地:5432 / DBNAME
当我在交互式python shell中运行db.create_all()时,它不会抛出任何错误,但它也没有做任何事情。
根据我的理解,它应该创建具有三列的User表; 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()
答案 0 :(得分:2)
应该是确切的格式。
app.config['SQLALCHEMY_DATABASE_URI'] = "postgresql://postgres:postgres@localhost/DBNAME"
postgres
是用户名,postgres
是密码,localhost
是地址
答案 1 :(得分:0)
尝试使用网址中的postgresql
代替postgres
。
这是用于 JDBC 和 SQLAlchemy doc(http://docs.sqlalchemy.org/en/rel_0_9/dialects/postgresql.html)的表单。