Error: psycopg2.IntegrityError: column "modified" contains null values in social auth module?

Error: psycopg2.IntegrityError: column "modified" contains null values in social auth module?

我正在使用 Django 1.9 开发代码库,我正忙着准备升级到 1.10 的一切。

从 python 社交身份验证迁移到 python 社交身份验证应用程序 django 后,我遇到了 运行 问题。我已经使用找到的步骤 here

更新我的设置和 url 文件后,我 运行 出现以下错误。有谁知道我该如何解决这个问题?

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

这通常发生在您升级模块并且新模块的模型有新的约束时,在您的情况下

column "modified" contains null values

一种方法是只删除导致错误的实例。为此,转到迁移文件夹并手动删除具有 0009_auto_20191118_0520 类型名称的文件,您可以删除,可能是全部,但 0001_initial.py file。之后 运行 python ./manage.py make migrations social_django,它应该会更新您的数据库。

您还可以考虑使用 cmd

清除 social_django 应用程序的迁移历史记录
$ python manage.py migrate --fake social_django zero

按照教程 here 了解如何做到这一点。

编辑: 您可以通过降级到较低版本的社交身份验证应用程序 django 来克服这个问题。 Django 1.9 上的最新版本是 social-auth-app-django==3.1.0

我能够通过降级到较低版本的社交身份验证应用程序 django 来克服这个问题。我能够在 Django 1.9 上获得的最新版本是 social-auth-app-django==3.1.0

我遇到了完全相同的问题。 (pip install "python-social-auth==0.2.21" -> 在 INSTALLED_APPS 中设置 social.apps.django_app.default -> python manage.py migrate -> pip install python-social-auth[django] -> 在 INSTALLED_APPS 中设置 social_django -> python manage.py migrate = column "modified" contains null values).

起初我以为这是模型和迁移文件中的更改之间的冲突。例如。来自这里:

Looks like you added null=True after created migration file

但是,这看起来不对,因为 /usr/local/lib/python2.7/site-packages/social_django/models.py 不包括 null=True:

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

相反,我怀疑问题是在创建列时更新现有行的默认值为空,这会立即发生冲突。此处讨论:

Django Migrations Add Field with Default as Function of Model

我通过将 null=True 添加到 /usr/local/lib/python2.7/site-packages/social_django/migrations/0009_auto_20191118_0520.py 中的 modified 字段(或安装 social_django 的任何地方)并重新 运行 来解决这个错误迁移。

        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),
        ),

更好的方法可能是像 created 字段那样设置 default=django.utils.timezone.now