Database migration not working

Hello Developers,

I’m facing a problem I’ve never encountered before. I’m still unsure whether it’s a Django-induced bug or an issue with the code I wrote. When I make changes to models.py and run the expected commands, I get the message “No migrations to apply.” When I check via PGAdmin, migrations are not applied to the database. The makemigrations command fails to properly detect changes and create migration files.

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions, app
Running migrations:
  No migrations to apply.

Due to confidentiality agreements, I can’t share much content, but we have had to reset the database many times due to this issue. It’s really frustrating. A similar problem was discussed in the forum before, but I couldn’t gather much data from the 2022 post.

2022-Post

Here is the models.py:


class Report(models.Model):
    STATUS_CHOICES = (
        ("open", "Open"),
        ("close", "Close"),
    )

    PRIORITY_CHOICES = (
        (1, "1"),
        (2, "2"),
        (3, "3"),
        (4, "4"),
        (5, "5"),
    )

    RISK_LEVEL_CHOICES = (
        ("low", "Low"),
        ("medium", "Medium"),
        ("high", "High"),
        ("critical", "Critical"),
    )

    title = models.CharField(max_length=255)
    asset = models.CharField(max_length=255)
    code = models.CharField(max_length=50)
    priority = models.IntegerField(choices=PRIORITY_CHOICES)
    risk_level = models.CharField(max_length=50, choices=RISK_LEVEL_CHOICES)
    source = models.CharField(max_length=255)
    analyst = models.ForeignKey(
        User, on_delete=models.CASCADE, limit_choices_to={"role": "analyst"}
    )
    status = models.CharField(max_length=50, choices=STATUS_CHOICES)
    description = models.TextField()
    creation_date = models.DateTimeField(default=timezone.now)
    modification_date = models.DateTimeField(auto_now=True)
    confirmation = models.BooleanField(default=False)

    class Meta:
        indexes = [
            models.Index(fields=['title']),
            models.Index(fields=['creation_date']),
            models.Index(fields=['priority']),
            models.Index(fields=['risk_level']),
        ]

    def __str__(self):
        return self.title

I would appreciate any help or guidance on resolving this issue.

Try running makemigrations with specifying the app name.

One common cause of this situation is that your migrations directory in that app weren’t properly created (or mistakenly deleted). Putting the app name on the makemigrations command will ensure the directory exists.

are you use db router??

Hello, I am already using it and I have reviewed the documentation about migration.

Hello, we did not use a DB router in the project.

What changes are you making to your models.py file that you are expecting to see reflected in your migrations?

What is the output from a showmigrations command?

This may seriously constrain our ability to try and help you here. If this is a serious issue, I would recommend that you hire someone such that your agreements can be properly maintained.

Please describe what you mean by this.

Please provide the actual commands being run, don’t just describe them.

Also, it may be helpful if you identified what versions you’re using of Python and Django, and what operating system(s) and databases are being used. And, are there any major third-party packages involved?

Hello,

I have never encountered this problem before. The problem has been discussed in forums but the solutions I tried did not solve the problem.

This is the output when I run the showmigrations command.

python .\manage.py showmigrations

admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
 [X] 0003_logentry_add_action_flag_choices
app
 [X] 0001_initial
 [X] 0002_alter_customuser_email_alter_customuser_username    
 [X] 0003_alter_malwaresample_signature
 [X] 0004_alter_forumtopic_title
 [X] 0005_alter_forumtopic_forum_name_and_more
 [X] 0006_malwaresample_scanned
 [X] 0007_rename_modified_date_exploitrecord_modified_and_more
auth
 [X] 0001_initial
 [X] 0002_alter_permission_name_max_length
 [X] 0003_alter_user_email_max_length
 [X] 0004_alter_user_username_opts
 [X] 0005_alter_user_last_login_null
 [X] 0006_require_contenttypes_0002
 [X] 0007_alter_validators_add_error_messages
 [X] 0008_alter_user_username_max_length
 [X] 0009_alter_user_last_name_max_length
 [X] 0010_alter_group_name_max_length
 [X] 0011_update_proxy_permissions
 [X] 0012_alter_user_first_name_max_length
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
sessions
 [X] 0001_initial

I do the same as in the other posts. I clear the django_migrations table from the database and run the makemigrations command again. When I run the migrate command, it asks me to clear the other tables.

python manage.py migrate

Operations to perform:
  Apply all migrations: admin, app, auth, contenttypes, sessions
Running migrations:
  Applying admin.0001_initial...Traceback (most recent call last):
  File "C:\Users\Usename\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\db\backends\utils.py", line 103, in _execute
    return self.cursor.execute(sql)
           ^^^^^^^^^^^^^^^^^^^^^^^^
psycopg2.errors.DuplicateTable: relation "django_admin_log" already exists

The command works successfully when I delete the database completely and create it again

python .\manage.py migrate 

Operations to perform:
  Apply all migrations: admin, app, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying sessions.0001_initial... OK

Version information I am using (I will be happy to provide more information if needed)

python --version
Python 3.12.4

django --version
5.1rc1

Operating System
Windows 10 22H2

Database
PostgreSQL 15

First, these two elements have no direct relationship to each other. Emptying the migrations table does not at all affect your need (or non-need) to run makemigrations.

The migrations table is not written by makemigrations, it is written by the migrate command.

This is, in general, a bad idea unless you know what to do from this point. (You would need to run your migrations one by one, using the --fake parameter to re-mark a migration as having been run without actually trying to perform the sql updates.)

Ignoring everything you’ve done before now - it looks like you have a database with migrations that have successfully run.

Is there a current issue needing to be addressed?

Note for future reference: Do not delete anything related to migrations without ensuring you understand why you are doing it, and how it affects the migrations environment. Do not delete migration files. Do not empty the migrations table. Otherwise, you are likely going to put yourself in a position where your easiest solution is to delete the database and reinitialize it.

1 Like

I understand the problem better. Thank you for your support.