django 1.7 migrate gets error “table already exist

2019-01-10 08:41发布

问题:

I am trying to apply a migration but am getting the error:

django.db.utils.OperationalError: (1050, "Table 'customers_customer' already exists")

I get this by issuing the following command:

python manage.py migrate

My customer table already exists, so what do I do to let the migration know this, not error out, and run my modification to my model?

I ran this on my local environment with local database with no problem. It is when I pointed my database to production and ran migrate above that I get this error.

回答1:

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-south or python manage.py help migrate



回答2:

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



回答3:

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