简体   繁体   中英

Modify Django migrations file, or use --fake flag?

I am new to Django and I am working on renaming some old tables. I am also taking some fields that already exist in a few tables, and making them foreign keys. After making the changes, and running makemigrations I went into the newly created migrations file and noticed that Django is attempting to make a new model. Then it is trying to delete the old model. I think the issue is it is attempting to create a new model before deleting the old model. If I try to run migrate right now it throws me an error saying:

django.db.utils.ProgrammingError: ('42S01', "[42S01] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]There is already an object named 'tblFailureTypes' in the database. (2714) (SQLExecDirectW)")

My migrations file looks like:

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

    dependencies = [
        ('foo', '0001_initial'),
    ]

    operations = [
        migrations.CreateModel(
            name='FailureType',
            fields=[
                ('id', models.AutoField(db_column='FailureID', primary_key=True, serialize=False)),
                ('failure_type', models.CharField(db_column='Failure Type', max_length=255, null=True)),
            ],
            options={
                'db_table': 'tblFailureTypes',
                'managed': True,
            },
        ),
        migrations.DeleteModel(
            name='FailureTypes',
        ),

I have seen two possible workarounds:

  1. Use the --fake flag when running migrate, but I have seen a lot of comments that this can cause problems later down the road. (I am also thinking --fake may not work here because there are many other migrations happening that I do not want to fake.)
  2. Go into the migrations file and manually switch the migrations.DeleteModel to happen before the migrations.CreateModel

My question is which one of these would be the preferred solution here? Am I correct in thinking --fake is not going to be an option? I do not currently have permission to make changes to the migrations file. So I am a bit nervous to give myself permissions to it. It seems like there may be a reason I don't have the permission. Can modifying the migrations file manually cause a problem, or is that going to be my best solution in this case?

  • If you are talking about working on local, simply, delete all the migrations and makemigrations again.

  • If you are on production, you are safe to use --fake once.

  • You are OK also to change the old migrations if you know how to do deal with migrations, and if it is few files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM