在 Django 中添加社交应用程序时的 IntegrityError:“provider_id”列中的 null 值

IntegrityError when Adding Social Application in Django: Null Value in Column "provider_id"

提问人:Laurie Crean 提问时间:11/12/2023 最后编辑:Laurie Crean 更新时间:11/13/2023 访问量:55

问:

在 Django 中添加社交应用程序时出现 IntegrityError:“provider_id”列中的 Null 值违反了非 null 约束”

尝试使用 Django Allauth 添加社交应用程序时,我在 Django 应用程序中遇到了 IntegrityError。该错误特别指向列中的空值,我正在寻求帮助来解决此问题。provider_id

当我尝试通过 Django 管理面板添加新的社交应用程序时,会发生错误。以下是详细的错误消息:($ 符号是为了保护隐私而阻止的

IntegrityError at /admin/socialaccount/socialapp/add/
null value in column "provider_id" violates not-null constraint
DETAIL:  Failing row contains (8, google, google, 888982092733-51qdar9ln6i5onjoli2blj9bb6$$$$$$$.apps.googleusercon..., GOCSPX-im5A3HBsy_5FFvJTKqz$$$$$$$, , null, null).

provider to Google,这是唯一的下拉选项

所有迁移都已在代码中进行

Google OAuth via django 详细信息

授权重定向 URI 链接到端口 URL。

发布状态为“生产中”。

正在咨询的资源。

Akamai 开发版本。“设置 Google 登录以获得更快的 Django 登录体验壮举。YouTube,YouTube 视频,2022 年 12 月 12 日,https://youtu.be/yO6PP0vEOMc?feature=shared&t=1386。2023年11月11日访问。

  • 在本教程中,已遵循所有步骤来实现 Google OAuth,添加社交帐户时会出现问题 23:06 左右。

“姜戈。”姜戈项目, 2023, https://docs.djangoproject.com/en/4.2/topics/auth/.2023年11月11日访问。

  • 根据本文档,Django OAuth 似乎已正确安装在 settings.py 中

代码存储库和合理的问题区域

https://github.com/lmcrean/Project-4 是代码存储库 -

问题区域可能是

  • codestar/settings.py - 但经过严格的拼写检查。也许是INSTALLED_APPS和SOCIALACCOUNT_PROVIDERS(如下所示)。尝试了其他两个版本的SOCIALACCOUNT_PROVIDERS,这似乎不是问题所在。
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.sites',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.google',
    'cloudinary_storage',
    'django.contrib.staticfiles',
    'cloudinary',
    'django_summernote',
    'crispy_forms',
    'blog',
    'users'
]

SOCIALACCOUNT_PROVIDERS = {
    "google": {
        "SCOPE": [
            "profile",
            "email"
        ],
        "AUTH_PARAMS": {"access_type": "online"}
    }
}

Django 管理员可用站点

当我尝试添加社交应用程序时,我有 2 个可用站点,我想知道这些站点现在是否可能是错误的,但我看不出它们可能有什么问题。

django oauth-2.0 django-admin google-oauth

评论

0赞 willeM_ Van Onsem 11/12/2023
您是否创建了自定义模型/管理员?如果是,请共享代码。
0赞 Laurie Crean 11/12/2023
没有自定义模型,没有自定义管理员

答:

0赞 GH00N 11/12/2023 #1

嗯,提供者是社会专家吗?或者你把表名改成了“提供者”?

如果您使用“Google 社交登录”的提供商

为此,可以在社交应用程序对象上设置 provider 字段。

from allauth.socialaccount.models import SocialAccount, SocialApp

social_app = SocialApp.objects.create(
    provider='google',
    name='Google',
    client_id=YOUR_GOOGLE_CLIENT_ID,
    secret_key=YOUR_GOOGLE_SECRET_KEY,
)

你可以通过在项目的 settings.py 文件中设置 SOCIALACCOUNT_PROVIDERS 设置来将其添加到你的 Django 项目中。例如:

# settings.py
SOCIALACCOUNT_PROVIDERS = {
    'google': {
        'PROVIDER': 'allauth.socialaccount.providers.google.GoogleProvider',
        'APP': {
            'client_id': YOUR_GOOGLE_CLIENT_ID,
            'secret_key': YOUR_GOOGLE_SECRET_KEY,
        }
    }
}

像这样你必须

但是,如果你的意思是表名是“提供者”,你必须分享你的代码

评论

0赞 Laurie Crean 11/12/2023
github.com/lmcrean/Project-4 是我刚刚尝试在python shell上运行该命令的代码存储库,并收到或多或少相同的错误消息 - NotNullViolation:“provider_id”列中的空值违反了非空约束
0赞 GH00N 11/12/2023
SOCIALACCOUNT_PROVIDERS = { “google”: { “SCOPE”: [ “profile”, “email” ], “AUTH_PARAMS”: {“access_type”: “online”} } } #here 你必须写
0赞 Laurie Crean 11/13/2023 #2

我已经解决了。

django-allauth 从 0.44.0 升级到最新的 0.59.0

  1. pip uninstall django-allauth # uninstalls 0.44.0
  2. pip install django-allauth # installs 0.59.0 by default

安装旧版本是出于一个非常具体的原因,以使一些 html 工作,我有点惊讶,但它现在适用于最新版本。


然后,当进入社交应用程序时,表单中有“provider_id”标签,在错误消息中将其描述为 null。

然后在 django admin 上输入详细信息

  • 提供商:Google(下拉列表)
  • 提供商 ID:google
  • 名称:Google SSO
  • 客户端 ID:[您的客户端 ID]
  • 密钥:[您的密钥在这里]
  • 无论您使用什么网站,都会添加为网站。

然后 Google 登录按预期工作

使用终端进行了一些迁移,并由终端指示合并一些旧的python3 manage.py makemigrations


我将在这里为其他人记录,对于创建Google登录,在终端错误解决oauth重定向错误后仅剩下的问题

Error 400: redirect_uri_mismatch

You can't sign in to this app because it doesn't comply with Google's OAuth 2.0 policy.

If you're the app developer, register the redirect URI in the Google Cloud Console.
Request details: redirect_uri=http://8000-lmcrean-project4-avaw7dd1zq8.ws-eu106.gitpod.io/accounts/google/login/callback/
Related developer documentation

Django 试图使用

http://8000-lmcrean-project4-avaw7dd1zq8.ws-eu106.gitpod.io/accounts/google/login/callback/

誓言 URI 是:

https://8000-lmcrean-project4-avaw7dd1zq8.ws-eu106.gitpod.io/accounts/google/login/callback/

因此,OAuth 被更改为匹配 Djangohttp://

执行此操作还需要将“发布状态”从 OAuth 同意屏幕上移回 。ProductionTesting