Dino Geek, try to help you

How to perform a database migration in Django?


Database migration in Django is a critical process that allows you to make changes to your database schema without the need to delete or recreate the database. Django achieves this using its built-in ORM (Object-Relational Mapping) library.

The key commands for migrations are:

- `makemigrations`: This is responsible for creating new migrations based on the changes detected on your models.
- `migrate`: This command is responsible for applying and unapplying migrations.

Django uses a migration file to keep track of changes to your models so it can intelligently apply those changes to the database schema.

It is worth mentioning that you will need to perform a database migration whenever you make changes to the `models.py` file, which includes events like creating a new model, changing field names, adding or removing fields, etc.

To begin:

1. Amend `models.py`: Start by modifying the `models.py` file. Let’s assume we’ve added a new field “date\_created” in a model “User”.

```
from django.db import models

class User(models.Model): name = models.CharField(max_length=200) date_created = models.DateTimeField(auto_now_add=True) # newly added field
```
1. Run `makemigrations`: Now you need to create a new migration file with `python manage.py makemigrations`. This will create a new migration file in your app’s “migrations” directory. The migration file contains the changes needed to your database schema in order to match the current state of your models.

1. Run `migrate`: Next, apply these changes to the database schema using `python manage.py migrate`. This command will execute SQL statements that modify your database schema (and possibly existing data).

1. Verification: To verify if the migration was successful, you can use Django’s database API to interact with the model and observe the changes. For instance, you can add a new user and then retrieve it:

```
from django.utils import timezone
from .models import User

  1. create a new user
    new_user = User(name=‘Example User’, date_created=timezone.now())
    new_user.save()
  1. retrieve the user
    retrieved_user = User.objects.get(name=‘Example User’)
    print(retrieved_user.date_created)
    ```

Remember, best practice suggests committing your migration files into version control along with the rest of your Django app.

More detailed information on Django migrations can be found in the Django documentation (https://docs.djangoproject.com/en/4.0/topics/migrations/).

Please carefully consider and test your migrations, especially in a production environment, as modifying your database schema can lead to data loss if not done correctly.


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use