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
8
votes
1 answer

Data migration only executed for the first test

I have a simple data migration, which creates a Group, and which looks like this : def make_manager_group(apps, schema_editor): Group = apps.get_model("auth", "Group") managers_group = Group(name="managers") managers_group.save() class…
BriceP
  • 508
  • 2
  • 13
8
votes
1 answer

Column 'django_migrations.id' has unsupported type 'serial' [ with Amazon Redshift]

I use django_celery with connecting to Amazon Redshift. To migrate database, after "makemigrations" I used command "python manage.py migrate" and error message show up as shown below. The reason is Redshift does not support data type 'serial' but…
8
votes
2 answers

How to create per-project initial_data fixtures in Django 1.7+

Before Django 1.7 I used to define a per-project fixtures directory in the settings: FIXTURE_DIRS = ('myproject/fixtures',) and use that to place my initial_data.json fixture storing the default groups essential for the whole project. This has been…
knaperek
  • 2,113
  • 24
  • 39
7
votes
2 answers

Django logging during migration

I have numerous complex data migrations set up in Django and would like to set up logging to see how they complete and catch errors. I could do this with regular python logging but Django has this semi-built-in and I was hoping the Django logging…
Austin Fox
  • 121
  • 1
  • 8
7
votes
1 answer

How do you cleanly maintain django migrations on non-master branch?

Imagine that I have a branch that has a migration. It's an experimental branch that keeps up with master, but will not be merged into it for a while, if ever. Master migrations change over time. The result is that when I merge in master, I end up…
Doug Bradshaw
  • 1,452
  • 1
  • 16
  • 20
7
votes
3 answers

How can I set/provide a default value while django migration?

Scenario: I have a model, Customer class Customer(models.Model): name = models.CharField(max_length=100) age = models.IntegerField() company = models.CharField(max_length=100) and now I updated the company attribute witha ForeignKey…
JPG
  • 82,442
  • 19
  • 127
  • 206
7
votes
0 answers

How to get a Django migration involving a unique index applied across foreign keys to go backwards?

Today I was tasked with fixing a stack of migrations so they can go fully forwards and backwards from scratch, without relying on starting from a database dump from production. I ran across this. class Migration(migrations.Migration): dependencies…
7
votes
3 answers

How to make migrations only one app inside project using django?

I have the follow project structure: MyProject |--myBaseApp | |--migrations | |--__init__.py | |--models.py | |... |--myClientApp | |--migrations | |--__init__.py | |--models.py | |... |--myProject | |--__init__.py | |--settings.py | …
7
votes
1 answer

How to squash all migration files in to one in django?

I have 82 migration files in my django app. Some of them are related to removed models. I want to merge all these 82 migration files into one migration file. How do I do without deleting the database?
user7236255
7
votes
1 answer

Why Django migration alter field (AlterField) that is not touched?

When i do migration python manage.py makemigrations wall i see in console that Django (1.8.12) tells me a long list of fields that are touched: Migrations for 'wall': 0079_auto_20170302_0024.py: - Add field periodic_task_interval to…
Vic Nicethemer
  • 1,081
  • 3
  • 16
  • 38
7
votes
2 answers

Migration of Django field with default value to PostgreSQL database

https://docs.djangoproject.com/en/1.10/topics/migrations/ Here it says: "PostgreSQL is the most capable of all the databases here in terms of schema support; the only caveat is that adding columns with default values will cause a full rewrite of the…
Ali Ankarali
  • 2,761
  • 3
  • 18
  • 30
7
votes
4 answers

How to remove a default value function in Django

In a comment to this question answer I asked how to remove a field with a default value function. To sum up, the example code is: def get_deadline(): return datetime.today() + timedelta(days=20) class Bill(models.Model): name =…
beruic
  • 5,517
  • 3
  • 35
  • 59
7
votes
1 answer

ValueError: path is on mount 'C:', start on mount 'F:' while django migrations in windows

I am trying to run the following command python manage.py makemigrations But, getting the error ValueError: path is on mount 'C:', start on mount 'F:' What can be the reason? complete traceback:- Traceback (most recent call last): File…
ankit
  • 1,499
  • 5
  • 29
  • 46
7
votes
5 answers

OperationalError: no such table

So I was working on my app and added a slugfield to my models. Then as normal went ahead to makemigrations, and a massive red wall of errors appeared. Traceback (most recent call last): File…
7
votes
1 answer

Django model for a Postgres view

Edit: There seems to be some confusion about what I'm asking. That model is for the Postgres view that I created in migration 0009. I was under the impression that Django won't generate a migration for a model if it has the managed = False option.…
NJP
  • 815
  • 1
  • 7
  • 20