flask-sqlalchemy
flask-sqlalchemy copied to clipboard
SQLite databases are not created in the instance directory
In Flask-SQLAlchemy 2.5.1, when I create a SQLite database connection with a relative path, the database is created in the application root directory rather than the instance directory. It looks like this should have changed in #537, but while that code was merged into main in May 2020 it looks like it has not been included yet in a release.
Actual Behavior
config.py:
SQLALCHEMY_DATABASE_URI = 'sqlite:///development.db'
The file development.db is created in app.root_path. (In this case, ./my-package, which is under source control.)
Expected Behavior
The file development.db is created in app.instance_path. (In this case ./instance, which is git-ignored.)
Environment:
- Python version: 3.9.5
- Flask-SQLAlchemy version: 2.5.1
- SQLAlchemy version: 1.4.23
Same for me: I had to dig for two hours until I found the crucial line of code in flask_alchemy, which surprisingly changed the meaning of my explicitly relative sqlite URI.
btw: it did not help in my hunt for the root cause, that the change of the sqlite URI was hidden within the following innocent looking line of code:
options = self.get_options(sa_url, echo)
I understand, that this specific interpretation of a relative path is supposed to improve the predictability of the filename for a deployment (where the deployer is often not aware of the state of cwd). But I consider this to be a solved problem: anyone deploying an application simply does not use relative paths, because they are used to the concept of absolute paths being preferable in configuration files.
Thus this behavior is probably not helpful for deployment environments. And in an development environment (like mine) this behavior really hurts, since people expect relative paths to be relative (based on the current directory). I find it very problematic to break this assumption.
Please reconsider this specific behavior. I am quite confident, that it causes a lot of confusion for only little benefit.
Thank you for your time!
This will be available when Flask-SQLAlchemy 3.0 is released. As open source volunteers with limited free time, we do not make statements on when a release will happen. It should be soon, given that I'm devoting my attention to it right now.