3

I am working on a codebase using Django 1.9, I am busy getting everything ready to upgrade to 1.10.

I have run into an issue after migrating from python social auth to python social auth app django. I have used the steps found here

After updating my settings and url files, I ran into the below error. Does anyone know how I can get around this?

Running migrations:
  Rendering model states... DONE
  Applying social_django.0006_partial... OK
  Applying social_django.0007_code_timestamp... OK
  Applying social_django.0008_partial_timestamp... OK
  Applying social_django.0009_auto_20191118_0520...Traceback (most recent call last):
  File "/home/brendan/venvs/social/lib/python3.6/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
psycopg2.IntegrityError: column "modified" contains null values


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/brendan/venvs/social/lib/python3.6/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/home/brendan/venvs/social/lib/python3.6/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/brendan/venvs/social/lib/python3.6/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/brendan/venvs/social/lib/python3.6/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/home/brendan/venvs/social/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/home/brendan/venvs/social/lib/python3.6/site-packages/django/db/migrations/executor.py", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/brendan/venvs/social/lib/python3.6/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/brendan/venvs/social/lib/python3.6/site-packages/django/db/migrations/executor.py", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/brendan/venvs/social/lib/python3.6/site-packages/django/db/migrations/migration.py", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/brendan/venvs/social/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards
    field,
  File "/home/brendan/venvs/social/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 396, in add_field
    self.execute(sql, params)
  File "/home/brendan/venvs/social/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 110, in execute
    cursor.execute(sql, params)
  File "/home/brendan/venvs/social/lib/python3.6/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/brendan/venvs/social/lib/python3.6/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/home/brendan/venvs/social/lib/python3.6/site-packages/django/db/utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/brendan/venvs/social/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/home/brendan/venvs/social/lib/python3.6/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: column "modified" contains null values
AzyCrw4282
  • 7,222
  • 5
  • 19
  • 35
Brendan
  • 180
  • 2
  • 15

3 Answers3

2

This usually happens when you upgrade the module and the newer module's models have new constraints, in your case

column "modified" contains null values

One way is to delete just the instance that are causing the error. For this, go to the migrations folder and delete manually files that have 0009_auto_20191118_0520 type of name, you can delete, probably all, but 0001_initial.py file. After that run python ./manage.py make migrations social_django, it should update your database.

You can also consider clearing the migration history for the social_django app, with the cmd

$ python manage.py migrate --fake social_django zero

Follow the tutorial here on how you can do that.

Edit: You can overcome this by downgrading to a lower version of social auth app django. The latest version was to get to on Django 1.9 was social-auth-app-django==3.1.0

AzyCrw4282
  • 7,222
  • 5
  • 19
  • 35
  • Thanks very much. When you say delete the migration files manually, do you mean in the social django module in my virtual environment? – Brendan Jul 29 '20 at 07:28
  • 1
    I have tried editing the module's migrations, adding in the kwarg null=True. This seems to have worked, however I am concerned that this is going to cause issues for anyone cloning the repo, and doesn't seem like the safest way to prepare production for the upgrade. – Brendan Jul 29 '20 at 08:33
  • I had added an answer here, but it was your answer that put me on the right track. If you edit your answer to include that social-auth-app-django==3.1.0 solves the problem I will accept it as the answer. – Brendan Jul 31 '20 at 14:23
0

I was able to overcome this by downgrading to a lower version of social auth app django. The latest version I was able to get to on Django 1.9 was social-auth-app-django==3.1.0

Brendan
  • 180
  • 2
  • 15
0

I hit the exact same issue. (pip install "python-social-auth==0.2.21" -> set social.apps.django_app.default in INSTALLED_APPS -> python manage.py migrate -> pip install python-social-auth[django] -> set social_django in INSTALLED_APPS -> python manage.py migrate = column "modified" contains null values).

At first I thought this was a conflict between the model and the changes in the migrations file. E.g. from here:

django.db.utils.IntegrityError: column "venue_city" contains null values

Looks like you added null=True after created migration file

However, that doesn't look right because /usr/local/lib/python2.7/site-packages/social_django/models.py does not include null=True:

    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)

Instead, I suspect the issue is the default value to update existing rows when creating the column is null which immediately conflicts. This is discussed here:

Django Migrations Add Field with Default as Function of Model

I got past this error by adding null=True to the modified field in /usr/local/lib/python2.7/site-packages/social_django/migrations/0009_auto_20191118_0520.py (or wherever social_django is installed) and re-running migrations.

        migrations.AddField(
            model_name='usersocialauth',
            name='created',
            field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='usersocialauth',
            name='modified',
-           field=models.DateTimeField(auto_now=True),
+           field=models.DateTimeField(auto_now=True, null=True),
        ),

A better way might be to set default=django.utils.timezone.now as is already done for the created field.

jozxyqk
  • 16,424
  • 12
  • 91
  • 180