sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) relation “story” does not exist

落爺英雄遲暮 提交于 2019-12-20 04:19:42

问题


So this is my code below. I'm trying to create a database, with one story table. The input comes from the html input part

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)

when I try to run this, I get the following error:

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'}]

The story table is not even created, I checked it through pgAdmin. I've tried a lot of things, some questions suggested to drop the table, but it's not created


回答1:


Have you followed the quickstart guide for Flask and sqlalchemy? Anyway, on the guide you will notice that it says to do this:

To create the initial database, just import the db object from an interactive Python shell and run the SQLAlchemy.create_all() method to create the tables and database:

>>> from yourapplication import db
>>> db.create_all()

In the code you included with your question the table creation code seems to be missing, which explains the table not being created.

You can consider including db.create_all() with your application (put it right after db = SqlAlchemy(app)) - if this wrapper functions like the standard sqlalchemy version it should only create new tables and not blow up if tables already exist.



来源:https://stackoverflow.com/questions/39191127/sqlalchemy-exc-programmingerror-psycopg2-programmingerror-relation-story-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!