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:
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.