sqlalchemy.exc.ProgrammingError:(psycopg2.ProgrammingError)关系"故事"不存在

时间:2016-08-28 12:10:44

标签: python flask flask-sqlalchemy

所以这是我的代码。我试图用一个故事表创建一个数据库。输入来自html输入部分

from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
from flask import request, redirect, url_for

app = Flask(__name__)
password = input("Your database password: ")
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://adambodnar:{}@localhost/user_stories'.format(password)
db = SQLAlchemy(app)

class Story(db.Model):
   id = db.Column(db.Integer, primary_key=True)
   story_title = db.Column(db.String(80), unique=True)
   user_story = db.Column(db.Text)
   acceptance_criteria = db.Column(db.Text)
   business_value = db.Column(db.Integer)
   estimation = db.Column(db.Integer)
   status = db.Column(db.String(30))

   def __init__(self, story_title, user_story, acceptance_criteria, business_value, estimation, status):
        self.story_title = story_title
        self.user_story = user_story
        self.acceptance_criteria = acceptance_criteria
        self.business_value = business_value
        self.estimation = estimation
        self.status = status


@app.route('/')
def index():
    return render_template('form.html')


@app.route('/story', methods=['POST'])
def story_post():
    new_story = Story(request.form['story_title'],request.form['user_story'], request.form['acceptance_criteria'], request.form['business_value'], request.form['estimation'], request.form['status'])


    db.session.add(new_story)
    db.session.commit()
    return redirect(url_for('index'))

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000)

当我尝试运行此操作时,出现以下错误:

sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) relation "story" does not exist
LINE 1: INSERT INTO story (story_title, user_story, acceptance_crite...
                ^
 [SQL: 'INSERT INTO story (story_title, user_story, acceptance_criteria, business_value, estimation, status) VALUES (%(story_title)s, %(user_story)s, %(acceptance_criteria)s, %(business_value)s, %(estimation)s, %(status)s) RETURNING story.id'] [parameters: {'acceptance_criteria': 'asdasd', 'estimation': '1', 'user_story': 'asd', 'status': 'Planning', 'story_title': 'asd', 'business_value': '100'}]

故事表甚至没有创建,我通过pgAdmin检查了它。我已经尝试了很多东西,有些问题建议放弃,但它没有创建

1 个答案:

答案 0 :(得分:2)

你有没有关注Flask和sqlalchemy的quickstart guide?无论如何,在指南上你会注意到它说:

  

要创建初始数据库,只需从中导入db对象   交互式Python shell并运行SQLAlchemy.create_all()方法   创建表和数据库:

$("#results").empty();    
for (var i = 0; i < 10; i++) {
     $("#results").append("<div class=\"resultContent\">" + "<h2>" + "<a href=\"" + results[3][i] + "\">" + results[1][i] + "</a>" + "</h2>" + "<p>" + results[2][i] + "<br/>" + "</p>")
}

在你的问题中包含的代码中,表创建代码似乎缺失,这解释了表没有被创建。

您可以考虑将>>> from yourapplication import db >>> db.create_all() 包含在您的应用程序中(在db.create_all()之后立即执行) - 如果此包装程序的功能类似于标准sqlalchemy版本,则它应该只创建新表,如果表已经存在则不会爆炸