django recreate dropped table

Viewed 2393

I dropped a table manually cause while changing the model I made a very bad mistake. So anyway ... how can I recreate it ? I have tried :

- Delete the migrate folder of that app
- delete from django_migrations where app='main'
- makemigrations main
- migrate main

But it stops at the first table. Which is there :-( and my Table message in that case is not recreated. In 0001_initial.py I have the correct entry for my table

migrations.CreateModel(
        name='Message',
        fields=[
            ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
            ('created_at', models.DateTimeField(auto_now_add=True)),
            ('updated_at', models.DateTimeField(auto_now=True)),
            ('unread', models.BooleanField(default=True)),
            ('subject', models.CharField(max_length=120)),
            ('freetext', models.TextField(null=True)),
            ('from_user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='from_user', to=settings.AUTH_USER_MODEL)),
            ('to_user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='to_user', to=settings.AUTH_USER_MODEL)),
        ],
    ),

Is there way to start this manually ?

2 Answers

I think best way is to write your own migration file.

Under the migration folder create new migration file with DeleteModel method. for instance if your previous migration file name is '0065_auto_20210318_0000' so your file migth be:

class Migration(migrations.Migration):

    dependencies = [
        ('my_app', '0065_auto_20210318_0000'),
    ]

    operations = [
        migrations.DeleteModel(
            name='MyModel',
        ),
    ]
Related