django-migrations

How to write migration to change primary key of model with ManyToManyField

断了今生、忘了曾经 提交于 2019-12-01 19:36:04
问题 I have a UserProfile model that refers to my User model with a OneToOneField . I also use the post_save signal to auto create the UserProfile when the user is created. This works great apart from when creating a user through the admin (where I use an inline) when I get an error about duplicate profile. This answer recommends setting the primary key to be the OneToOneField referring to user. So before: class UserProfile(models.Model): user = models.OneToOneField(settings.AUTH_USER_MODEL) # ...

Django migration: making a row not primary key, getting “ProgrammingError: multiple default values”?

流过昼夜 提交于 2019-12-01 16:29:49
I am working with Postgres on a Django app and I want to make a model change, resetting a row so it is no longer the primary key. This is what my model currently looks like in Django: class Meeting(TimeStampedModel): row = models.CharField(max_length=20, primary_key=True) total_items = models.IntegerField() I have run django-admin.py flush to remove all data from the database. If I run python manage.py makemigrations I see No changes detected . So we are starting from a clean base. Now I edit row in models.py so it is no longer the primary key: row = models.CharField(max_length=20) And run

How can I send signals from within Django migrations?

ε祈祈猫儿з 提交于 2019-12-01 15:32:10
I use Django 1.7 migrations, and in particular, want to populate a newly-created database with initial data. Thus, I use a data migration for this. It looks like this: def populate_with_initial_data(apps, schema_editor): User = apps.get_model("auth", "User") new_user = User.objects.create(username="nobody") class Migration(migrations.Migration): ... operations = [ migrations.RunPython(populate_with_initial_data), ] At the same time, I want to have an instance of the UserDetails model for every new user: @receiver(signals.post_save, sender=django.contrib.auth.models.User) def add_user_details

Received “ValueError: Found wrong number (0) of constraints for …” during Django migration

和自甴很熟 提交于 2019-12-01 13:47:31
问题 While using Django 1.7 migrations, I came across a migration that worked in development, but not in production: ValueError: Found wrong number (0) of constraints for table_name(a, b, c, d) This is caused by an AlterUniqueTogether rule: migrations.AlterUniqueTogether( name='table_name', unique_together=set([('a', 'b')]), ) Reading up on bugs and such in the Django bug DB it seems to be about the existing unique_together in the db not matching the migration history. How can I work around this

Unable to create the django_migrations table (ORA-02000: missing ALWAYS keyword)

大憨熊 提交于 2019-12-01 06:44:57
I'm starting a project in Django-2.0.1 with a database Oracle 11g, and when I run $python manage.py migrate, I get the error django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table (ORA-02000: missing ALWAYS keyword). You can see the full stack below: Traceback (most recent call last): File "/Users/user/anaconda3/lib/python3.6/site-packages/django/db/backends/utils.py", line 83, in _execute return self.cursor.execute(sql) File "/Users/user/anaconda3/lib/python3.6/site-packages/django/db/backends/oracle/base.py", line 500, in execute return self

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

。_饼干妹妹 提交于 2019-12-01 04:45:58
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 the 'django_migrations' table that contain 'serial' type is automatically created. How to stop Django Migrations create this table or avoid using serial on 'django_migrations' table. D:\code\test_celery_django>python manage.py migrate Traceback (most recent call last): File "manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "C:

Why does django 1.7 creates migrations for changes in field choices?

こ雲淡風輕ζ 提交于 2019-12-01 02:08:29
I have observed this behaviour on version 1.7 but not in previous versions using south migration. eg. class RedemptionCode(models.Model): EXPIRE_OPTIONS = ( ('1 week', '1 Week'), ) expire_option = models.CharField(max_length=255, choices=EXPIRE_OPTIONS) when I added more options: EXPIRE_OPTIONS = ( ('1 week', '1 Week'), ('2 weeks', '2 Weeks'), ('1 month', '1 Month'), ('1 day', '1 Day'), ) and run makemigrations , it creates a migration for it, coming from south background I thought it should say no changes detected as it doesn't affects database schema. I don't know what purpose it serves:

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

妖精的绣舞 提交于 2019-12-01 01:53:29
问题 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 the 'django_migrations' table that contain 'serial' type is automatically created. How to stop Django Migrations create this table or avoid using serial on 'django_migrations' table. D:\code\test_celery_django>python manage.py migrate Traceback (most

Django backup strategy with dumpdata and migrations

烂漫一生 提交于 2019-11-30 21:25:29
As in this question , I set up a dumpdata -based backup system for my database. The setup is akin to running a cron script that calls dumpdata and moves the backup to a remote server, with the aim of simply using loaddata to recover the database. However, I'm not sure this plays well with migrations . loaddata now has an ignorenonexistent switch to deal with deleted models/fields, but it is not able to resolve cases where columns were added with one-off defaults or apply RunPython code. The way I see it, there are two sub-problems to address: Tag each dumpdata output file with the current

How to switch the direction of a Django OneToOneField?

不打扰是莪最后的温柔 提交于 2019-11-30 21:13:37
I currently have a couple of models set up like this: from django.db import models class Child(models.Model): child_name = models.CharField(max_length=200) class Parent(models.Model): parent_name = models.CharField(max_length=200) child = models.OneToOneField(Child, null=True, blank=True) Unfortunately this is wrong, because I want a delete on the parent to cascade to the child and not vice-versa, so I really should have them set up like this: class Parent(models.Model): parent_name = models.CharField(max_length=200) class Child(models.Model): child_name = models.CharField(max_length=200)