# Activating models

That small bit of model code gives Django a lot of information. With it, Django is able to:

- Create a database schema (`<span class="pre">CREATE</span> <span class="pre">TABLE</span>` statements) for this app.
- Create a Python database-access API for accessing `<span class="pre">Question</span>` and `<span class="pre">Choice</span>` objects.

But first we need to tell our project that the `<span class="pre">polls</span>` app is installed.

> Philosophy
> 
> Django apps are “pluggable”: You can use an app in multiple projects, and you can distribute apps, because they don’t have to be tied to a given Django installation.

To include the app in our project, we need to add a reference to its configuration class in the [`<span class="pre">INSTALLED_APPS</span>`](https://docs.djangoproject.com/en/4.1/ref/settings/#std-setting-INSTALLED_APPS) setting. The `<span class="pre">PollsConfig</span>` class is in the `<span class="pre">polls/apps.py</span>` file, so its dotted path is `<span class="pre">'polls.apps.PollsConfig'</span>`. Edit the `<span class="pre">mysite/settings.py</span>` file and add that dotted path to the [`<span class="pre">INSTALLED_APPS</span>`](https://docs.djangoproject.com/en/4.1/ref/settings/#std-setting-INSTALLED_APPS) setting. It’ll look like this:

<div class="literal-block-wrapper docutils container" id="bkmrk-mysite%2Fsettings.py%C2%B6"><div class="literal-block-wrapper docutils container"><div class="code-block-caption"><span class="caption-text">`<span class="pre">mysite/settings.py</span>`</span>[¶](https://docs.djangoproject.com/en/4.1/intro/tutorial02/#id3 "Permalink to this code")</div><div class="highlight-python notranslate"><div class="highlight"></div></div></div></div>```
INSTALLED_APPS = [
    'polls.apps.PollsConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
```

Now Django knows to include the `<span class="pre">polls</span>` app. Let’s run another command:

<div class="console-block" id="bkmrk-%EF%85%BC%2F%EF%85%B9%C2%A0%EF%85%BA-%24-python-manag"><label for="c-tab-1-unix" title="Linux/macOS">/</label> <label for="c-tab-1-win" title="Windows"></label><section class="c-content-unix" id="bkmrk-%24-python-manage.py-m">```
$ python manage.py makemigrations polls
```

</section></div>You should see something similar to the following:

```
Migrations for 'polls':
  polls/migrations/0001_initial.py
    - Create model Question
    - Create model Choice
```

By running `<span class="pre">makemigrations</span>`, you’re telling Django that you’ve made some changes to your models (in this case, you’ve made new ones) and that you’d like the changes to be stored as a *migration*.

Migrations are how Django stores changes to your models (and thus your database schema) - they’re files on disk. You can read the migration for your new model if you like; it’s the file `<span class="pre">polls/migrations/0001_initial.py</span>`. Don’t worry, you’re not expected to read them every time Django makes one, but they’re designed to be human-editable in case you want to manually tweak how Django changes things.

There’s a command that will run the migrations for you and manage your database schema automatically - that’s called [`<span class="pre">migrate</span>`](https://docs.djangoproject.com/en/4.1/ref/django-admin/#django-admin-migrate), and we’ll come to it in a moment - but first, let’s see what SQL that migration would run. The [`<span class="pre">sqlmigrate</span>`](https://docs.djangoproject.com/en/4.1/ref/django-admin/#django-admin-sqlmigrate) command takes migration names and returns their SQL:

<div class="console-block" id="bkmrk-%EF%85%BC%2F%EF%85%B9%C2%A0%EF%85%BA-%24-python-manag-0"><label for="c-tab-2-unix" title="Linux/macOS">/</label> <label for="c-tab-2-win" title="Windows"></label><section class="c-content-unix" id="bkmrk-%24-python-manage.py-s">```
$ python manage.py sqlmigrate polls 0001
```

</section></div>You should see something similar to the following (we’ve reformatted it for readability):

```
BEGIN;
--
-- Create model Question
--
CREATE TABLE "polls_question" (
    "id" bigint NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
    "question_text" varchar(200) NOT NULL,
    "pub_date" timestamp with time zone NOT NULL
);
--
-- Create model Choice
--
CREATE TABLE "polls_choice" (
    "id" bigint NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
    "choice_text" varchar(200) NOT NULL,
    "votes" integer NOT NULL,
    "question_id" bigint NOT NULL
);
ALTER TABLE "polls_choice"
  ADD CONSTRAINT "polls_choice_question_id_c5b4b260_fk_polls_question_id"
    FOREIGN KEY ("question_id")
    REFERENCES "polls_question" ("id")
    DEFERRABLE INITIALLY DEFERRED;
CREATE INDEX "polls_choice_question_id_c5b4b260" ON "polls_choice" ("question_id");

COMMIT;
```

Note the following:

- The exact output will vary depending on the database you are using. The example above is generated for PostgreSQL.
- Table names are automatically generated by combining the name of the app (`<span class="pre">polls</span>`) and the lowercase name of the model – `<span class="pre">question</span>` and `<span class="pre">choice</span>`. (You can override this behavior.)
- Primary keys (IDs) are added automatically. (You can override this, too.)
- By convention, Django appends `<span class="pre">"_id"</span>` to the foreign key field name. (Yes, you can override this, as well.)
- The foreign key relationship is made explicit by a `<span class="pre">FOREIGN</span> <span class="pre">KEY</span>` constraint. Don’t worry about the `<span class="pre">DEFERRABLE</span>` parts; it’s telling PostgreSQL to not enforce the foreign key until the end of the transaction.
- It’s tailored to the database you’re using, so database-specific field types such as `<span class="pre">auto_increment</span>` (MySQL), `<span class="pre">bigint</span> <span class="pre">PRIMARY</span> <span class="pre">KEY</span> <span class="pre">GENERATED</span> <span class="pre">BY</span> <span class="pre">DEFAULT</span> <span class="pre">AS</span> <span class="pre">IDENTITY</span>` (PostgreSQL), or `<span class="pre">integer</span> <span class="pre">primary</span> <span class="pre">key</span> <span class="pre">autoincrement</span>` (SQLite) are handled for you automatically. Same goes for the quoting of field names – e.g., using double quotes or single quotes.
- The [`<span class="pre">sqlmigrate</span>`](https://docs.djangoproject.com/en/4.1/ref/django-admin/#django-admin-sqlmigrate) command doesn’t actually run the migration on your database - instead, it prints it to the screen so that you can see what SQL Django thinks is required. It’s useful for checking what Django is going to do or if you have database administrators who require SQL scripts for changes.

If you’re interested, you can also run [`<span class="pre">python</span> <span class="pre">manage.py</span> <span class="pre">check</span>`](https://docs.djangoproject.com/en/4.1/ref/django-admin/#django-admin-check); this checks for any problems in your project without making migrations or touching the database.

Now, run [`<span class="pre">migrate</span>`](https://docs.djangoproject.com/en/4.1/ref/django-admin/#django-admin-migrate) again to create those model tables in your database:

<div class="console-block" id="bkmrk-%EF%85%BC%2F%EF%85%B9%C2%A0%EF%85%BA-%24-python-manag-1"><label for="c-tab-3-unix" title="Linux/macOS">/</label> <label for="c-tab-3-win" title="Windows"></label><section class="c-content-unix" id="bkmrk-%24-python-manage.py-m-0">```
$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, polls, sessions
Running migrations:
  Rendering model states... DONE
  Applying polls.0001_initial... OK
```

</section></div>The [`<span class="pre">migrate</span>`](https://docs.djangoproject.com/en/4.1/ref/django-admin/#django-admin-migrate) command takes all the migrations that haven’t been applied (Django tracks which ones are applied using a special table in your database called `<span class="pre">django_migrations</span>`) and runs them against your database - essentially, synchronizing the changes you made to your models with the schema in the database.

Migrations are very powerful and let you change your models over time, as you develop your project, without the need to delete your database or tables and make new ones - it specializes in upgrading your database live, without losing data. We’ll cover them in more depth in a later part of the tutorial, but for now, remember the three-step guide to making model changes:

- Change your models (in `<span class="pre">models.py</span>`).
- Run [`<span class="pre">python</span> <span class="pre">manage.py</span> <span class="pre">makemigrations</span>`](https://docs.djangoproject.com/en/4.1/ref/django-admin/#django-admin-makemigrations) to create migrations for those changes
- Run [`<span class="pre">python</span> <span class="pre">manage.py</span> <span class="pre">migrate</span>`](https://docs.djangoproject.com/en/4.1/ref/django-admin/#django-admin-migrate) to apply those changes to the database.

The reason that there are separate commands to make and apply migrations is because you’ll commit migrations to your version control system and ship them with your app; they not only make your development easier, they’re also usable by other developers and in production.

Read the [<span class="doc">django-admin documentation</span>](https://docs.djangoproject.com/en/4.1/ref/django-admin/) for full information on what the `<span class="pre">manage.py</span>` utility can do.