6

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!

1

2 Answers 2

6

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

Sign up to request clarification or add additional context in comments.

2 Comments

It's better to create the database from within the app. The Flask docs explain how to do that easily.
Here's an example of what @dotslash is talking about: github.com/hypatia-software-org/staticfuzz/blob/master/…
0

What helped me in the the same scenario where the db file wasn't created correctly is to run from terminal:

  1. In terminal type command:

    flask init-db

  2. Then and then run the app again with command:

    flask run

This updated the example db file correctly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.