Why only id column is shown in migration file when I create model in Python?

Viewed 66

from django.db import models

class Town(models.Model):

     name: models.CharField(max_length=70,unique=True)
     country: models.CharField(max_length=30,unique=True)

     class Meta:
         pass

This is my model Town whith two attributes: name and country. When I create a migration in the initial_0001.py file only id column is shown

from django.db import migrations, models

class Migration(migrations.Migration):

    initial = True

dependencies = [
]

operations = [
    migrations.CreateModel(
        name='Town',
        fields=[
            ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
        ],
    ),
] 

What kind of problem could it be?

1 Answers

The ID-Field is always automatically generated by Django when making migrations. You can specifiy your own ID field aswell, but using an auto-incremented like this is fine for your use case.

You also might want to get rid of the unique=True, as it would prevent adding multiple towns from the same country.

Create your model like this and redo your migration process:

class Town(models.Model):

     name = models.CharField(max_length=70)
     country = models.CharField(max_length=30)

When you have 'no changes detected' when making migrations, I usually follow these steps:

First, Delete migrations folder in project.

Then delete django-migration entries in your Database with this query:

DELETE from django_migrations WHERE app='yourAppName'

Then, create new folder ‘migrations” in app folder + init.py

Lastly, redo the migration process:

py manage.py makemigrations
py manage.py migrate --run-syncdb
Related