Forums

Help!! sqlite3.OperationalError: no such table: users

Hi guys,

I'm new ish to python and am following a Udemy course for REST API's in Flask.

I keep getting the error message mentioned in the title when I try and send a POST request to the auth endpoint. The full message is below.

 2019-06-07 12:49:20,070: Exception on /auth [POST]
Traceback (most recent call last):
  File "/usr/lib/python3.7/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/lib/python3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/lib/python3.7/site-packages/flask_restful/__init__.py", line 273, in error_router
    return original_handler(e)
  File "/usr/lib/python3.7/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/usr/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/collardesolate/.local/lib/python3.7/site-packages/flask_jwt/__init__.py", line 122, in _default_auth_request_handler
    identity = _jwt.authentication_callback(username, password)
  File "/home/collardesolate/mysite/security.py", line 6, in authenticate
    user = User.find_by_username(username)
  File "/home/collardesolate/mysite/user.py", line 16, in find_by_username
    result = cursor.execute(query, (username,))
sqlite3.OperationalError: no such table: users

I'm not running a virtualenv. I have 100% already created the table. I have downloaded the DB file and viewed it in an SQLite viewer and I can see the rows there and it is called 'users'.

Please can anyone help? I have googled this as well as searching on here but couldn't find anything Flask specific and am so confused because I have 100% created the table it is looking for before I run the POST request. I have also gone forward and did copy all the code from the Udemy course directly and still hit with a 500 error, so I'm thinking it might be something PythonAnywhere specific?

As a newbie I'm not quite sure what code you would need to help debug this and I don't want to clog this post up with loads of code for now hoping that the PythonAnywhere team can just view my files. Please ask if you want the file and PythonAnywhere staff, you have my permission to view my files!

Thanks very much in advance.

You're probably using a relative path to your database file without paying attention to what the working directory may be. See our documentation here about how to avoid that issue.

Hi Glen, the db file is in the same folder as the app files and the rest of them too. And if it couldn't access it at all wouldn't I get a different error message instead of 'no table found'?

Sorry if I'm sounding like a total newbie, but that ^ is my current understanding. If it's in the same folder then no need to assign another path to it? Is that wrong?

Thanks

I will read the documentation though. Thanks

It doesn't matter if it's in the same folder. Relative paths are relative to the working directory, not the the file they're referenced in and sqlite does not complain if a database file does not exists, it just gives you an empty database.

Resolved! Thanks a bunch, and for the explanation too.

No problem ;)

sqlite3.OperationalError: no such table:

app = Flask(__name__)
app.secret_key = "I WONT SHOW U MY SECRET KEY ON FORUMS!"
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.sqlite3'
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.permanent_session_lifetime = timedelta(minutes=20)

db = SQLAlchemy(app)


def __init__(self, name, email):
    self.name = name
    self.email = email


class users(db.Model):
    _id = db.Column("id", db.Integer, primary_key=True)
    name = db.Column(db.String(100))
    email = db.Column(db.String(100))

[edit by admin: formatting]

.

app = Flask(__name__)
app.secret_key = "5223"
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.sqlite3'
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.permanent_session_lifetime = timedelta(minutes=20)

db = SQLAlchemy(app)


def __init__(self, name, email):
    self.name = name
    self.email = email


class users(db.Model):
    _id = db.Column("id", db.Integer, primary_key=True)
    name = db.Column(db.String(100))
    email = db.Column(db.String(100))

[edit by admin: formatting]

You're using a relative path to your SQLite database, so your code is dependent on the working directory that the running process happens to have; see this help page for hints on how to fix that.

Run this command python manage.py migrate --run-syncdb after this command python manage.py migrate

I don't think that that would help in this case -- the OP is using Flask, not Django.