12
The migration you are trying to run is under the impression that a certain column exists in the database that isn’t actually there, since you removed it at some point. So you’ll need to roll back the migrations through faking to “convince” the database that the migration is there, and then reapply the migrations.
Say you have these migrations in your app myapp
:
- 0001_initial.py
- 0002_something.py
- 0003_removedcolumn.py
- 0004_anotherthing.py
You’re going to need to roll back the migrations back to a point when the column still existed. Note that if you are in a prod database, you should be aware of potential data loss that could come from doing this.
If you removed the column in 0003_removedcolumn.py
, you’ll need to run: python manage.py migrate --fake myapp 0002_something
.
Then, you can rerun the sequential migrations: python manage.py migrate myapp
. Your problem should be solved now!
From here, you can run python manage.py makemigrations
and it should run smoothly.
21
I had the same issue. Basically, the reason is because the migration thinks the database has those columns but the DB actually does not, so you need a procedure to delete those non-existent columns from migration records.
1.Comment those columns in your code.
2.Reset migrations.
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc" -delete
3.Do normal initial migration.
python manage.py makemigrations
python manage.py migrate
4.In your code, uncomment those duplicate columns.
python manage.py makemigrations
python manage.py migrate --fake
5.Now your migrations and code are on same page. I use fake to let migration believe DB has those column, but indeed DB does not.
6.In your code, comment those duplicate column again.
python manage.py makemigrations
python manage.py migrate
7.Here, you successfully delete those column records from migrations. And also in your code, those column are commented. They are on the same page.
8.Uncomment those columns again in your code and do migrations.
python manage.py makemigrations
python manage.py migrate
9.It should then work.
This is the only way that worked for my situation. I hope others can provide an easier approach.
- [Django]-Pytest.mark.parametrize with django.test.SimpleTestCase
- [Django]-Django admin TabularInline – is there a good way of adding a custom html column?
- [Django]-Django: Open uploaded file while still in memory; In the Form Clean method?
6
I got the same problem.
I just tried python3 manage.py migrate appname --fake
from https://github.com/BirkbeckCTP/janeway/issues/451 and it’s work for me.
- [Django]-Django multiple template inheritance – is this the right style?
- [Django]-In django, how do I sort a model on a field and then get the last item?
- [Django]-What are the limitations of Django's ORM?
4
I’m going to assume that you have a previous migration in which you’ve already added the short_description_eng
field. You could check this by looking through the previous migration files for the string 'short_description_eng'
. If thats True, you can just delete the following from the 0002_auto_20160315_1544 migration file.
migrations.AddField(
model_name='words',
name='short_description_eng',
field=models.CharField(blank=True, default='', max_length=100, verbose_name='Условное обозначение Eng'),
),
If that isn’t the case then its possible you’ve got your database in a foobar’ed state and it may be worth deleting it (assuming this is a development database) and recreating it.
- [Django]-FileUploadParser doesn't get the file name
- [Django]-How can I serialize a queryset from an unrelated model as a nested serializer?
- [Django]-Handle `post_save` signal in celery
1
When I had this issue, I had to go to each migration that said it had a duplicate column and merge the 0002_*.py
migration file with the 0001_initial.py
migration file.
1. Copy all the operations in the operations = []
list and paste them into the operations = []
list in the 0001_initial.py
file.
migrations.AddField(
model_name='words',
name='short_description_eng',
field=models.CharField(blank=True, default='', max_length=100, verbose_name='Условное обозначение Eng'),
),
migrations.AddField(
model_name='words',
name='short_description_rus',
field=models.CharField(blank=True, default='', max_length=100, verbose_name='Условное обозначение Рус'),
),
migrations.AddField(
model_name='words',
name='short_description_tur',
field=models.CharField(blank=True, default='', max_length=100, verbose_name='Условное обозначение Tür'),
),
migrations.AlterField(
model_name='words',
name='audio',
field=models.FileField(blank=True, upload_to='audio', verbose_name='Озвучка'),
),
migrations.AlterField(
model_name='words',
name='english',
field=models.TextField(blank=True, default='', verbose_name='English'),
),
migrations.AlterField(
model_name='words',
name='russian',
field=models.TextField(blank=True, default='', verbose_name='Русский'),
),
migrations.AlterField(
model_name='words',
name='title',
field=models.CharField(max_length=100, unique=True, verbose_name='Слово'),
),
migrations.AlterField(
model_name='words',
name='turkish',
field=models.TextField(blank=True, default='', verbose_name='Türkçe'),
),
2. If you had other dependencies besides 0001_initial
, you would copy all the lines in dependencies = []
(except the line that says('myapp', '0001_initial'),
) migrations from the list of dependencies and paste them into the 0001_initial.py
migration file.
Important Note: Make sure you grab any import dependencies too. I had no issues after following those simple steps.
- [Django]-Timestamp fields in django
- [Django]-Django proxy model and ForeignKey
- [Django]-How to get superuser details in Django?
1
-
Remove the field of ‘short_description_tur’ from DB
by using the bellow script:Alter table Words Drop short_description_tur;
-
And then
migrate
- [Django]-Allowing RabbitMQ-Server Connections
- [Django]-Django ignores router when running tests?
- [Django]-Django migration fails with "__fake__.DoesNotExist: Permission matching query does not exist."
0
Just found the solution! I deleted all my migrations with 0001_initial
, and then run makemigrations
and migrate
and all the changes apllied!
- [Django]-Pagination in Django-Rest-Framework using API-View
- [Django]-Django.db.utils.IntegrityError: duplicate key value violates unique constraint "django_migrations_pkey"
- [Django]-Is there a way to filter a queryset in the django admin?
0
I saw this problem when I re-ran a partially applied migrations scripts. The solution was to temporarily delete the lines that added the columns that had already been created by the new schema (somewhat hacky but works for development environment databases)
- [Django]-Django 1.7 – App 'your_app_name' does not have migrations
- [Django]-Django: show the count of related objects in admin list_display
- [Django]-Homepage login form Django