Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using SQLite URI filename format for in-memory database still creates a file #1314

Open
Sparrow0hawk opened this issue Feb 18, 2024 · 2 comments · May be fixed by #1315
Open

Using SQLite URI filename format for in-memory database still creates a file #1314

Sparrow0hawk opened this issue Feb 18, 2024 · 2 comments · May be fixed by #1315

Comments

@Sparrow0hawk
Copy link

Sparrow0hawk commented Feb 18, 2024

Outline

When using the SQLite URI filename format for an in memory database sqlite:///file::memory:?uri=true Flask-SQLAlchemy still creates a database file on disk.

MRE

You can replicate this behaviour locally with the following code:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import DeclarativeBase, mapped_column, Mapped

class Base(DeclarativeBase):
    pass

db = SQLAlchemy(model_class=Base)

class User(db.Model):
    id: Mapped[int] = mapped_column(primary_key=True)
    username: Mapped[str] = mapped_column(unique=True)
    email: Mapped[str]

app = Flask(__name__)

app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///file::memory:?uri=true"

db.init_app(app)

with app.app_context():
    db.create_all()

@app.route("/")
def index():
    return "<h1>Hello world</h1>"

If you start the app with flask run --debug you'll find you have an instance/:memory: file on disk.

Expected behaviour

Running the above snippet does not produce a file on disk.

Environment:

  • Python version: Python 3.11.7
  • Flask-SQLAlchemy version: 3.1.1
  • SQLAlchemy version: 2.0.27
@Sparrow0hawk Sparrow0hawk linked a pull request Feb 18, 2024 that will close this issue
6 tasks
@Sparrow0hawk Sparrow0hawk changed the title Using SQLite URI format for in-memory database still creates a file Using SQLite URI filename format for in-memory database still creates a file Feb 18, 2024
@davidism
Copy link
Member

This looks weird, because :memory: is not a file. Is this actually valid or suggested syntax?

@Sparrow0hawk
Copy link
Author

There are examples on SQLite docs of using :memory: as a URI filename with the syntax file::memory: so I believe it is an accepted syntax.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants