django migration table does not exist

13,118
  1. drop tables,
  2. comment-out the model in model.py,
  3. if you are using django version >= 1.7:

python manage.py makemigrations

python manage.py migrate --fake

else

python manage.py schemamigration someapp --auto

python manage.py migrate someapp --fake

  1. comment-in your model in models.py
  2. go to step 3. BUT this time without --fake

Reference: https://stackoverflow.com/a/27583836/4359237

Share:
13,118
varad
Author by

varad

Updated on June 17, 2022

Comments

  • varad
    varad about 2 years

    I have added new model to my app. I did makemigration and in my migration I can see the code to create my models like:

    operations = [
        migrations.CreateModel(
            name='Blog',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('title', models.CharField(max_length=120)),
                ('body', models.TextField()),
                ('post_date', models.DateTimeField(default=django.utils.timezone.now)),
                ('like', models.IntegerField(default=0)),
                ('created_by', models.ForeignKey(to=settings.AUTH_USER_MODEL)),
            ],
            options={
            },
            bases=(models.Model,),
    

    Now when I do python manage.py migrate it gives me error saying table does not exist..

    Why I am getting this error. It should have been migrated right ? How to fix this issue?

  • jozxyqk
    jozxyqk almost 7 years
    I have just created a new database with no tables. All management commands say 'Table myapp.myapp_mymodel doesn't exist'.. well yeah. The problem was a Form's forms.ModelChoiceField(queryset=MyModel.staticFunction() running on module load. Wrapping the static call with lazy(...) solved it.