db.create_all() on flask doesn't create database file

Question:

I’m unable to create a SQLite database file with Flask-SQLAlchemy. This was working before. It doesn’t show an error or warning, but it doesn’t create the SQLite file. I also tried creating the database from the terminal but that didn’t work either.

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + 'os.path.join(basedir,data.sqlite)'
db = SQLAlchemy(app)

from model import Sample
from app import db, app

class Sample(db.Model):
    id_data = db.Column(db.Integer, primary_key=True)
    data = db.Column(db.Float(80), index=True, unique=False)

with app.app_context():
     db.create_all()
Asked By: Riccardo-99

||

Answers:

Flask-SQLAlchemy uses the Flask app’s instance path for SQLite relative paths.

Usually, current working directory + instance, your SQLite file should be in there.

From the docs:

# SQLite, relative to Flask instance path
sqlite:///project.db

[…]

SQLite does not use a user or host, so its URLs always start with three slashes instead of two. The dbname value is a file path. Absolute paths start with a fourth slash (on Linux or Mac). Relative paths are relative to the Flask application’s instance_path.

Answered By: ljmc

I had the same issue but then I tried with windows, the exact same code, ran flawlessly. I guess there’s a problem with file permissions of macoss system.

I’ve spent almost 2 days on this problem. I guess, maybe we should move this topic to another category related to Linux-unix-mac topics.

Answered By: erdoganb