‘NOT NULL constraint failed’ after adding to models.py
You must create a migration, where you will specify default value for a new field, since you don’t want it to be null. If null is not required, simply add null=True and create and run migration.
You must create a migration, where you will specify default value for a new field, since you don’t want it to be null. If null is not required, simply add null=True and create and run migration.
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 stage. … Read more
I realize this question is old and at the time the best option for Data Migrations was using South. Now Django has its own migrate command, and the process is slightly different. I’ve added these models to an app called books — adjust accordingly if that’s not your case. First, add the field to Book … Read more
Model methods are not available in migrations, including data migrations. However there is workaround, which should be quite similar to calling model methods. You can define functions inside migrations that mimic those model methods you want to use. If you had this method: class Order(models.Model): ”’ order model def goes here ”’ def get_foo_as_bar(self): new_attr=”bar: … Read more
Finally got it to work, although I don’t know why and I hope it will work in the future. After doing numerous trials and going through Django’s dev site (link). Here are the steps (for whoever runs into this problem): Empty the django_migrations table: delete from django_migrations; For every app, delete its migrations folder: rm … Read more
Here is an example doing everything in one single migration thanks to a RunPython call. # -*- coding: utf-8 -* from __future__ import unicode_literals from django.db import migrations, models import uuid def create_uuid(apps, schema_editor): Device = apps.get_model(‘device_app’, ‘Device’) for device in Device.objects.all(): device.uuid = uuid.uuid4() device.save() class Migration(migrations.Migration): dependencies = [ (‘device_app’, ‘XXXX’), ] operations … Read more
For whatever it’s worth the the creators of Django recommend PostgreSQL. If you’re not tied to any legacy system and have the freedom to choose a database back-end, we recommend PostgreSQL, which achives a fine balance between cost, features, speed and stability. (The Definitive Guide to Django, p. 15)
Shell The only simple solution I’ve found so far is running ./manage.py showmigrations | grep ‘\[ \]’ which will output an empty string in case all migrations have been applied. However, it is closely tied to the output format. Python I checked the source code of migrate command and it seems like this should do … Read more
I would just do the following on both the environments (as long as the code is the same) Delete your migrations folder DELETE FROM django_migrations WHERE app = <your app name> . You could alternatively just truncate this table. python manage.py makemigrations python manage.py migrate –fake After this all your changes should get detected across … Read more
I just learned how to do this with a single migration! When running makemigrations django should ask you to set a one-off default. Define whatever you can here to keep it happy, and you’ll end up with the migration AddField you mentioned. migrations.AddField( model_name=”series”, name=”updated_as”, field=models.DateTimeField(default=????, auto_now=True), preserve_default=False, ), Change this one operation into 3 … Read more