7

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?

varad
  • 7,309
  • 20
  • 60
  • 112

1 Answers1

10
  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

Community
  • 1
  • 1
sk1pro99
  • 967
  • 1
  • 12
  • 23
  • 2
    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. – jozxyqk Aug 03 '17 at 22:28