Opened 40 minutes ago
#37521 assigned Bug
SQLite backend creates a "file:" directory tree for URI database names
| Reported by: | Mariano Ignacio Baragiola | Owned by: | Mariano Ignacio Baragiola |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | dev |
| Severity: | Release blocker | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
The SQLite backend always connects with uri=True, so a NAME that starts with file: is a SQLite URI. Since 2f7f30c320 (#36496), DatabaseWrapper.get_new_connection() treats the name as a path and runs Path(name).parent.mkdir(parents=True) before it connects. For a URI name this creates a file: directory tree in the current working directory. If that directory is read-only, the connection fails.
Reproduction (an existing database, read-only working directory):
DATABASES = { "default": { "ENGINE": "django.db.backends.sqlite3", "NAME": "file:/srv/data/app.sqlite3?mode=ro", } }
from django.db import connection connection.cursor().execute("SELECT 1")
Actual:
- writable working directory: the query works, but Django creates the directories
file:/srv/data/in the working directory. - read-only working directory:
django.db.utils.InterfaceError: Error creating path to SQLite database: [Errno 13] Permission denied: 'file:'.
Expected: Django opens the database as in 6.1 and creates no directories.
Versions: main only. stable/6.1.x is not affected.
Regression in 2f7f30c320 (#36496, PR #22030).
Suggested fix: skip the parent directory creation when the name starts with file:. In-memory names (:memory:, file:...?mode=memory) already skip it.
AI disclosure: I found and reproduced this with Claude Code (Anthropic).