django-migrations

How to reset migrations in Django 1.7

二次信任 提交于 2019-11-27 11:22:34
(I know there is a title the same as this, but the question is different). I have managed to get my development machine migrations and production migrations out of sync. I have a Django app which was using South. I had my own workflow that worked fine (it probably wasn't the correct way to do things, but I had no problems with it). Basically I have a script that copies the production database dump to my development machine. It also copied the migration files. That way the two were in synch, and I could run South commands as normal. Now I have upgraded to 1.7, and started using migrations. When

How to recreate a deleted table with Django Migrations?

 ̄綄美尐妖づ 提交于 2019-11-27 10:57:54
问题 There are two models Groups and Students and only one table for Groups of them, the Students table was deleted. How to maje Django create it again? If I do makemigrations it prints "No changes detected". On admin page when I click on the Students table it throws an exception: relation "students_students" does not exist 回答1: In django 1.7 you can try: 1. Delete your migrations folder 2. In the database: DELETE FROM django_migrations WHERE app = 'app_name'. You could alternatively just truncate

django 1.7 migrate gets error “table already exists”

微笑、不失礼 提交于 2019-11-27 10:55:20
I am trying to apply a migration but am getting the error: django.db.utils.OperationalError: (1050, "Table 'customers_customer' already exists") I get this by issuing the following command: python manage.py migrate My customer table already exists, so what do I do to let the migration know this, not error out, and run my modification to my model? I ran this on my local environment with local database with no problem. It is when I pointed my database to production and ran migrate above that I get this error. If you have the table created in the database, you can run python manage.py migrate -

How to add a new field to a model with new Django migrations?

◇◆丶佛笑我妖孽 提交于 2019-11-27 09:32:35
问题 I'm using the contribute_to_class method but I don't know how to create the field in the database with new migrations. 回答1: To answer your question, with the new migration introduced in Django 1.7 , in order to add a new field to a model you can simply add that field to your model and initialize migrations with ./manage.py makemigrations and then run ./manage.py migrate and the new field will be added to your DB. To avoid dealing with errors for your existing models however, you can use the -

django revert last migration

陌路散爱 提交于 2019-11-27 05:45:51
I've made a migration that added a new table and want to revert it and delete the migration, without creating a new migration. How do I do it? Is there a command to revert last migration and then I can simply delete the migration file? Alasdair You can revert by migrating to the previous migration. For example, if your last two migrations are: 0010_previous_migration 0011_migration_to_revert Then you would do: ./manage.py migrate my_app 0010_previous_migration You can then delete migration 0011_migration_to_revert . If you're using Django 1.8+, you can show the names of all the migrations with

Rerun a Django data migration

为君一笑 提交于 2019-11-27 05:32:24
问题 How would I rerun a data migration on Django 1.8+? If relevant, my migration is numbered 0011_my_data_migration.py and is the latest migration. 回答1: Fake back to the migration before the one you want to rerun. ./manage.py migrate --fake yourapp 0010_my_previous_data_migration Then rerun the migration. ./manage.py migrate yourapp 0011_my_data_migration Then you can fake back to the most recent migration that you have run. In your case, you said that 0011 was the latest, so you can skip this

Django data migration when changing a field to ManyToMany

风格不统一 提交于 2019-11-27 05:15:30
问题 I have a Django application in which I want to change a field from a ForeignKey to a ManyToManyField. I want to preserve my old data. What is the simplest/best process to follow for this? If it matters, I use sqlite3 as my database back-end. If my summary of the problem isn't clear, here is an example. Say I have two models: class Author(models.Model): author = models.CharField(max_length=100) class Book(models.Model): author = models.ForeignKey(Author) title = models.CharField(max_length=100

Adding django admin permissions in a migration: Permission matching query does not exist

微笑、不失礼 提交于 2019-11-27 03:15:28
问题 I wanted to add some groups and assign permissions to them in a manually written migration but if I run it on a clean DB it creates permissions only after running all migrations. I've found this ticket: https://code.djangoproject.com/ticket/23422 but I cannot comment there (it's possible I was banned after expressing some discontent with GeoDjango docs), so I'll share an improvement over the solution there below. 回答1: In django 1.10 the following code could be used: from django.contrib.auth

How to simplify migrations in Django 1.7?

痴心易碎 提交于 2019-11-27 02:55:23
There are already similar questions for South, but I have started my project with Django 1.7 and am not using South. During development a lot of migrations have been created, however the software is not yet delievered and there exists no database that must be migrated. Therefore I would like to reset the migrations as if my current model was the original one and recreate all databases. What is the recommended way to do that? EDIT: As of Django 1.8 there is a new command named squashmigrations which more or less solves the problem described here. kzorro I got this. I just figured this out and

Django migration with uuid field generates duplicated values

五迷三道 提交于 2019-11-27 02:02:43
问题 I have a uuid field (not a primary key). The generated migration is: from __future__ import unicode_literals from django.db import migrations, models import uuid class Migration(migrations.Migration): dependencies = [ .... ] operations = [ ... migrations.AddField( model_name='device', name='uuid', field=models.UUIDField(default=uuid.uuid4, unique=True), ), ... ] But when doing python manage.py migrate it is crashing with: django.db.utils.IntegrityError: could not create unique index