djangopostgresqlmakemigrations

Django Postgres makemigrations only Autofield at 0001_initial.py


Python 3.10.4 Django 4.0.5 PostgreSQL 14

When I start "python manage.py makemigrations" i got the file "0001_initial.py" but all Fields, except autofields, are missing.

models.py

from django.db import models

# Create your models here.

class Username(models.Model):
    #id = models.AutoField(primary_key=True)
    username: models.CharField(max_length=100)

class Carrier(models.Model):
    #id = models.AutoField(primary_key=True)
    carriername: models.CharField(max_length=100)
    desc: models.TextField()

0001_initial.py

# Generated by Django 4.0.5 on 2022-06-29 13:18

from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

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

Solution

  • First, you must know that Django By default adds the id field to the models ... Try to Delete the migration file and you must use the = not the : so it will be like this

    class Username(models.Model):
        username=models.CharField(max_length=100)
    
    class Carrier(models.Model):
        carriername = models.CharField(max_length=100)
        desc =  models.TextField()
    

    rerun manage.py makemigrations and it Should work