Django 1.7 shift 得到错误“ table already vis”

我试图应用一种迁移,但得到了一个错误:

Utils.OperationalError: (1050,“ Table‘ customer _ customer’” 已经存在”)

我通过发出以下命令得到这个结果:

python manage.py migrate

我的客户表已经存在了,那么我该怎么做才能让迁移知道这一点,不出错,并运行我对模型的修改呢?

我用本地数据库在我的本地环境中运行这个程序,没有任何问题。当我将数据库指向生产环境并在上面运行 migrate时,我得到了这个错误。

124600 次浏览

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

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