Questions tagged [django-migrations]

Django migrations are a way to apply changes to a database previously created, introduced in Django 1.7. This tool is used when a model is modified (adding a field, deleting a model, etc.) and you need to apply these changes to your database.

Migrations are introduced in Django 1.7 to make easier modifications in models:

Migrations are Django’s way of propagating changes you make to your models (adding a field, deleting a model, etc.) into your database schema. They’re designed to be mostly automatic, but you’ll need to know when to make migrations, when to run them, and the common problems you might run into.

There are several commands which you will use to interact with migrations and Django’s handling of database schema:

  • migrate, which is responsible for (un)applying migrations
  • makemigrations, to create new migrations files based on model modifications
  • sqlmigrate, which displays the SQL statements for a migration.
  • showmigrations, which lists a project’s migrations and their status.

This tag is not about migrating to Django from another framework. This is supposed to replace , which is a third-party mainly used until Django 1.6 to migrate models modifications.

1460 questions
0
votes
1 answer

Django migration, MyModel.objects.get() returning empty list

Using Django 1.8.2, I'm trying to make a migration on one model. Here is an exemple of the model: class Question(models.Model): module = models.ForeignKey(Module) my_order = models.PositiveIntegerField(default=0, blank=False, null=False) …
Xavier C.
  • 1,921
  • 15
  • 22
0
votes
1 answer

Migrations error in Django after moving to new server

I'm developing a Django 1.8 application locally and having reached a certain point a few days ago, I uploaded the app to a staging server, ran migrations, imported the sql dump, etc. and all was fine. I've since resumed local development which…
aperture
  • 2,905
  • 3
  • 35
  • 58
0
votes
1 answer

Migration in main project not detected

I have a main project, and some apps: proj ├── proj │   ├── admin.py │   ├── forms.py │   ├── __init__.py │   ├── models.py │   ├── settings.py │   ├── urls.py │   ├── views.py │   └── wsgi.py ├── static ├── manage.py ├── app1 ├── app2 └──…
blueFast
  • 41,341
  • 63
  • 198
  • 344
0
votes
2 answers

Django run migration before 3rd party app migration

I'm installing a 3rd party app, but needed to rename one of my apps as the names clashed. As part of this renaming I needed to write a migration to update django_content_type and django_migrations tables. The trouble is when the migrations run, the…
derbauio
  • 3
  • 2
0
votes
2 answers

External script and unapplied migrations

I am writing script supporting my Django project development. First I thought of using bash for this purpose but due to lack of enough knowledge and total lack of time I decided to write something using argparse and running system commands using…
hebius
  • 133
  • 1
  • 14
0
votes
0 answers

Python3 mysqlclient Django mysql migrate Permission denied

First time I tried to use Django and Mysql on Python 3. I Use mysqlclient library which is recommended by Django. But when I try to migrate, I got this error. I use OS X El Capitan and Python 3.5.1. Here's the…
0
votes
1 answer

ResetDjango postgresql database? flush does not work

I have made changes to my model and tried to migrate the database using: python3 manage.py makemigrations python3 manage.py migrate I got the following output: vagrant@vagrant-ubuntu-trusty-64:/vagrant/grader$ python3 manage.py makemigrations No…
user1283776
  • 19,640
  • 49
  • 136
  • 276
0
votes
1 answer

Delete migrations that haven't been migrated yet

I set a key that I have now realizes is wrong. It is set at migration 0005. The last migration I did was 0004. I'm now up to 0008. I want to rebuild the migrations with the current models.py against the current database schema. Migration 0005 is no…
User
  • 23,729
  • 38
  • 124
  • 207
0
votes
1 answer

Django migrations workflow

I have 3 migrations already thats in production Migration-1 Migration-2 Migration-3 Now, Im adding some dev work and constantly trying out different things and end up with 5 additional…
user1050619
  • 19,822
  • 85
  • 237
  • 413
0
votes
1 answer

How do I structure the `backwards` function in a Django migration without allowing null values in the database?

I'm implementing this answer to add an automatic UUIDField to a database with existing rows. The suggested adjustments to the data migration are: from django_extensions.utils import uuid def forwards(self, orm): for item in…
YPCrumble
  • 26,610
  • 23
  • 107
  • 172
0
votes
0 answers

Django run migrations over previous django project

I did a pg_dump on a Heroku app. Then I did a pg_restore on a local branch of the project that includes new models and fields. Then, I ran python manage.py makemigrations and I got the message no changes detected (weird). Then, when I ran python…
Alejandro Veintimilla
  • 10,743
  • 23
  • 91
  • 180
0
votes
2 answers

Django migrations best practice

I'm using Django 1.7 with migrations, and I'm not sure about what is the best practice, I should add the migrations files to my repository, or this is a bad idea?
0
votes
1 answer

Django Migrations - how to insert just one model?

I just made a mess in my local Django project and realized that somehow I'm out of sync with my migrations. I tried to apply initial and realized that some of the tables already exist, so I tried --fake. This made the migration pass, but now I'm…
d33tah
  • 10,999
  • 13
  • 68
  • 158
0
votes
2 answers

Django-cms 1.7.10 "OperationalError - no such column" after migration

So I know there are already a ton of questions by people who changed a model and then failed to apply the migration to their database. However, in my case, I know for a fact that the migration was applied, as I can see the new table data. Basically,…
Lèse majesté
  • 7,923
  • 2
  • 33
  • 44
0
votes
0 answers

How can I migrate a OneToOneField to the related model?

I have something like this: class A(Model): b = OneToOneField('B', related_name='a') class B(Model): pass But for obvious reasons I think my models would be better structured like this: class A(Model): pass class B(Model): a =…
jozxyqk
  • 16,424
  • 12
  • 91
  • 180