django 1.7 migrate gets error "table already exists" django 1.7 migrate gets error "table already exists" django django

django 1.7 migrate gets error "table already exists"


If you have the table created in the database, you can run

python manage.py migrate --fake <appname>

Mark migrations as run without actually running them

Or if you want to avoid some actions in your migration, you can edit the migration file under the app/migrations directory and comment the operations you don't want to do in the migrate execution.

Docs: https://docs.djangoproject.com/en/1.8/topics/migrations/#upgrading-from-southor python manage.py help migrate


Its actually python manage.py migrate --fake <appname>


We can solve this issue in two way as mentioned in answer: 1.) By editing in migration file

We have migrations folder created in each application we create, In those migration folder the migration file(0001_initial.py is the initially created and after this all other files dependent on this initial file will be create), When we run the python manage.py migrate, For every APP the migration file will apply if there is change in the file. We can see this run Applying on terminal after the migrate command. If there is any issue in migration file we use to get the error at that point. In my/our case:

Applying ValetUser.0002_keyroundslots_systemparameters_vehicleparking_vehicleparkingdetails...Traceback (most recent call last):sqlite3.OperationalError: table "valet_keyroundslots" already exists

Here we can notice that the file in which we have issue is mentioned i.e ValetUser.0002_keyroundslots_systemparameters, So we can Go to the App and then migrations and in 0002 file we can Comment the CreateModel Operation of That particular Model in which we are facing issue while applying migrations. example:

operations = [    # migrations.CreateModel(    #     name='KeyRoundSlots',    #     fields=[    #         ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),    #         ('key_round', models.IntegerField()),    #         ('key_slot', models.IntegerField()),    #         ('is_available', models.BooleanField()),    #         ('Valet_id', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='valet_location', to='ValetUser.ValetAt')),    #     ],    #     options={    #         'db_table': 'valet_keyroundslots',    #     },    # ),

2.) By applying fake migration of the modified migration file of the particular APP in which we are facing the error/issue, --fake will apply the fake migration that will not effect to the already applied migration of the model.

python manage.py migrate --fake <appname>

The answers given Waqas and elmonkeylp are also right, I just wanna explain it in brief with the help of we use to scenario