错误:psycopg2.IntegrityError:“已修改”列在社交身份验证模块中包含空值?
Posted
技术标签:
【中文标题】错误:psycopg2.IntegrityError:“已修改”列在社交身份验证模块中包含空值?【英文标题】:Error: psycopg2.IntegrityError: column "modified" contains null values in social auth module? 【发布时间】:2020-11-18 00:12:24 【问题描述】:我正在使用 Django 1.9 编写代码库,我正忙着准备升级到 1.10。
从 python social auth 迁移到 python social auth app 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
【问题讨论】:
【参考方案1】:这通常发生在您升级模块并且较新模块的模型有新约束时,在您的情况下
“已修改”列包含空值
一种方法是只删除导致错误的实例。为此,请转到迁移文件夹并手动删除名称类型为 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 模块中吗? 我尝试编辑模块的迁移,添加 kwarg null=True。这似乎奏效了,但我担心这会给任何克隆 repo 的人带来问题,而且似乎不是为升级准备生产的最安全方法。 我在这里添加了一个答案,但正是你的答案让我走上了正轨。如果您编辑答案以包含 social-auth-app-django==3.1.0 解决问题,我将接受它作为答案。【参考方案2】:我能够通过降级到较低版本的社交身份验证应用程序 django 来克服这个问题。我能够在 Django 1.9 上获得的最新版本是 social-auth-app-django==3.1.0
【讨论】:
【参考方案3】:我遇到了完全相同的问题。 (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
)。
起初我以为这是模型和迁移文件中的更改之间的冲突。例如。从这里开始:
django.db.utils.IntegrityError: column "venue_city" contains null values
看起来您在创建迁移文件后添加了 null=True
但是,这看起来不对,因为/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)
相反,我怀疑问题是在创建列时更新现有行的默认值是 null,这会立即发生冲突。此处讨论:
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
(或安装了 social_django 的任何位置)的 modified
字段并重新运行迁移来解决此错误。
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),
),
更好的方法可能是设置 default=django.utils.timezone.now
,就像对 created
字段所做的那样。
【讨论】:
以上是关于错误:psycopg2.IntegrityError:“已修改”列在社交身份验证模块中包含空值?的主要内容,如果未能解决你的问题,请参考以下文章