Skip to content
Back to skills

Django Queries

ASecurity

Write Django ORM queries, raw SQL, and transactions that work on Sentry's production databases. Use when writing or reviewing database access code in Sentry. Not for migration files (use generate-migration) or model design (use django-models).

  • 45,908 stars
  • 0 votes
  • 0 copies
  • 3 views
  • Added December 1, 2026
devopspythongosqldjangogitapidatabase

Works with

  • api

Security analysis

A100/100

Scanned December 1, 2026

npx -y skills add getsentry/sentry --skill django-queries --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Django Queries?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Django Queries
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/getsentry-django-queries/badge)](https://www.skillsdirectory.com/skills/getsentry-django-queries)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: django-queries
description: Write Django ORM queries, raw SQL, and transactions that work on Sentry's production databases. Use when writing or reviewing database access code in Sentry. Not for migration files (use generate-migration) or model design (use django-models).
---

# Django Queries on Sentry's Databases

## Tables on Separate Databases

Within a silo, production keeps some tables on separate Postgres databases. The router picks the connection per model, so single-table ORM calls just work, but one SQL statement or one transaction cannot span two databases. Tests rarely catch this: Sentry's test router (`TestSiloMultiDatabaseRouter` in `src/sentry/db/router.py`) splits out only the cron monitor tables.

The table-to-database map is `_default_db_routing_map` in getsentry's `getsentry/db/router.py`. Unlisted tables are on `default`, and `SENTRY_DB_ROUTING_MAP_OVERRIDES` in `getsentry/conf/settings/` adds per-environment entries. Read it from a getsentry checkout (usually a sibling of this repo) or with `gh api -H "Accept: application/vnd.github.raw" repos/getsentry/getsentry/contents/getsentry/db/router.py`. If you cannot read getsentry, say so instead of assuming the tables share a database.

Look up every table involved before you:

- **Query more than one table in one statement**: `id__in=<queryset>`, `Subquery`/`Exists`, a join across a foreign key, or raw SQL. Across databases, run separate queries and pass IDs through Python (`list(qs.values_list("id", flat=True))`). For raw SQL, take the connection from the router (`connections[router.db_for_read(Model)]`), not `django.db.connection`.
- **Write to more than one model in a `transaction.atomic` block**: it only covers the database named in `using`, usually `router.db_for_write(Model)`.
- **Add a foreign key or a model**: reference a table on another database with a plain `BoundedBigIntegerField` ID column, as `ReleaseFile.release_id` does. A new table is on `default` unless it is added to the map.
- **Write a migration**: `RunPython` and `SafeRunSQL` also need routing hints. Use the **`generate-migration`** skill.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in devops →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.