Schema Migrations with Django
Django has built-in support for schema migrations. Below, we’ll walk through the process of setting up a Django project with Nile and running your first migration.
Install Django
If you don’t already have Django installed, you can install it using pip:
Create a new Django project
This will create a new Django project in the myproject
directory.
Create a new app
This will create a new Django app in the myproject
directory.
If all went well, the myproject
directory should now have the following structure:
Setting up Nile Database
Edit the myproject/settings.py
file to include the Nile database URL:
You can find your database credentials in the Nile dashboard.
Run the migrations built-in migrations
Django has several default modules that need database tables created. To create these tables, run the following command:
This will create the tables in the database.
Create a new model
In myapp/models.py
, create a new model:
And update the installed apps in myproject/settings.py
to include your app:
Create a new migration
To create a new migration, run the following command:
This will create a new migration file in the myapp/migrations
directory.
Run the migration
To run the migration, run the following command:
This will apply the migration to the database.
Connect to your database and see a new table created, called myapp_mymodel
.