Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flask/sqlalchemy - OperationalError: (sqlite3.OperationalError) no such table

I get the below error:

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: s_amodel [SQL: 'INSERT INTO s_amodel

My files forms.py

from flask_wtf import Form
class SAForm(Form):

models.py

from app.app_and_db import db 

class SAmodel(db.Model):
    id = db.Column(db.Integer(), primary_key=True)

views.py

from app.app_and_db import app, db
from app.sa.forms import SAForm
from app.sa.models import SAmodel

@sa_blueprint.route('/sa/new', methods=['GET','POST'])
def new_sa():

        form = SAForm(request.form, SAmodel)

        if request.method=='POST' and form.validate():
            model = SAmodel()
            form.populate_obj(model)
            db.session.add(model)
            db.session.commit()
            return redirect(url_for("sa.home_page"))
        return render_template("new_sa.html", form=form)

config.py

SQLALCHEMY_DATABASE_URI = os.getenv('DATABASE_URL', 'sqlite:///app.sqlite')

This is what I have. The app.sqlite is created but it is 0 bytes. When I submit something using new_sa() function, I get the above error. Also how is it getting the table name "s_amodel"?

Any help would be greatly appreciated. Thanks a lot!

like image 820
Potta Pitot Avatar asked May 18 '15 19:05

Potta Pitot


1 Answers

You're supposed to initialize/create the tables first. Please read the Creating the Database article in the official Flask documentation:

Such systems need a schema that tells them how to store that information. So before starting the server for the first time it’s important to create that schema.

Here's Flask's example of using a schema SQL script to create the database, tables, etc:

sqlite3 /tmp/flaskr.db < schema.sql

The recommended way is to use db.create_all() within your app. For example, see: https://github.com/lily-mayfield/staticfuzz/blob/d2e54186f5639a06a5a796f0499a984ca8919ed7/staticfuzz.py#L403

like image 132
Lillian Seabreeze Avatar answered Nov 04 '22 02:11

Lillian Seabreeze