Avatar of Dolamite Jenkins
Dolamite JenkinsFlag for United States of America asked on

I am trying to learn Flask but I can not get past this error with my sqlite database ...

from flask import Flask, render_template,request,redirect
import smtplib
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
import sqlite3


app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///friends.db' # Don't have this line below the next
db = SQLAlchemy(app)

#Create Model

class Friends(db.Model):
     id = db.Column(db.Integer,primary_key=True)
     name = db.Column(db.String(200),nullable = False)
     date_created = db.Column(db.DateTime, default=datetime.utcnow)


    #create String
     def __repr__(self):
        return '<Name %r>' % self.id


@app.route('/friends', methods =['POST' , 'GET'])
def friends():
    title = "Cmy friend"

    if request.method == "POST":
        friend_name = request.form['name']
        new_friend = Friends(name = friend_name)


        try:
            db.session.add(new_friend)
            db.session.commit()
            return redirect('/friends')
        except:
            return "there was ans error "

    else:
        friends = Friends.query.order_by(Friends.date_created)
        return render_template("friends.html",title=title, friends=friends)

Open in new window


{% block title %}{{ title }}{% endblock %}
{% block content %}
      <div class="container">
        <h1>Friends ...</h1>
        <br/>
          <form action="/>friends" METHOD="POST">
              <input type="text" placeholder="Name" name="name">
              <input type="=submit" value="Add Name"
                     class="btn btn-secoundary ">

          </form>
          <br/><br/>
          {% for friend in friends %}
            <h4>
                {{ friend.name}}
            </h4>
          {% endfor %}
      </div>
{% endblock %}

Open in new window

I have the database but when I run it I get the following error 

Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

Open in new window


This is the trace back
  File "/Users/clayrichmond/Dev/flaskWholeCourse/venv/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 717, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such column: friends.date_created [SQL: SELECT friends.id AS friends_id, friends.name AS friends_name, friends.date_created AS friends_date_created FROM friends ORDER BY friends.date_created] (Background on this error at: https://sqlalche.me/e/14/e3q8) 127.0.0.1 - - [07/Oct/2021 12:24:32] "GET /friends HTTP/1.1" 500 -

Open in new window

DatabasesSQL

Avatar of undefined
Last Comment
David Favor

8/22/2022 - Mon
lcohan

Did you had a look at what the link above https://sqlalche.me/e/14/e3q8  tells you about the operational error?
Essentially your database friends table does not have the date_created  column as per above error message: "no such column: friends.date_created" comming from the SQL Statement:
SELECT friends.id AS friends_id, friends.name AS friends_name, friends.date_created AS friends_date_created FROM friends ORDER BY friends.date_created

Open in new window

ASKER
Dolamite Jenkins

I did and I tried solving it ... the column is there see attached pix of database  ...then I tried deleting the query ... and still the error
ASKER CERTIFIED SOLUTION
David Favor

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck