Skip to content

Commit

Permalink
Create run_sql.md
Browse files Browse the repository at this point in the history
  • Loading branch information
williln authored Jul 23, 2024
1 parent 8882fcc commit d7e199a
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions django/run_sql.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Run SQL statements as part of your migrations with `migrations.RunSQL`

## Links

- [RunSQL](https://docs.djangoproject.com/en/5.0/ref/migration-operations/#runsql)

## Notes

`migrations.RunSQL` enables you to run raw SQL on your database from within your migration file.

From the docs:

You can also pass a list of strings or 2-tuples. The latter is used for passing queries and parameters in the same way as cursor.execute(). These three operations are equivalent:

```py
migrations.RunSQL("INSERT INTO musician (name) VALUES ('Reinhardt');")
migrations.RunSQL([("INSERT INTO musician (name) VALUES ('Reinhardt');", None)])
migrations.RunSQL([("INSERT INTO musician (name) VALUES (%s);", ["Reinhardt"])])
```

> I haven't used this myself, but learned about it from someone else and wanted to document it!

0 comments on commit d7e199a

Please sign in to comment.