social-app-django icon indicating copy to clipboard operation
social-app-django copied to clipboard

JSONField with JSONFieldBase from postgress doesn't support filtering

Open Mateusz-Slisz opened this issue 6 years ago • 1 comments

I don't know is it intended behavior or not, but I got django.db.utils.ProgrammingError when I was trying to simple filter UserSocialAuth by acces_token from extra_data.

Steps to reproduce

from social_django.models import UserSocialAuth

UserSocialAuth.objects.filter(extra_data__access_token="example")

traceback

psycopg2.ProgrammingError: operator does not exist: text -> unknown
LINE 1: ... WHERE ("social_auth_usersocialauth"."extra_data" -> 'access...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

SOCIAL_AUTH_POSTGRES_JSONFIELD is set up to True

Mateusz-Slisz avatar Jul 04 '19 21:07 Mateusz-Slisz

I had the same issue when migrating from SOCIAL_AUTH_POSTGRES_JSONFIELD = False to SOCIAL_AUTH_POSTGRES_JSONFIELD = True. You need to migrate the field from text field to a jsonb field. Here's a command or migration to do it:

ALTER TABLE social_auth_usersocialauth ALTER COLUMN extra_data SET DATA TYPE jsonb USING extra_data::jsonb;
from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('users', '__add_your_last_user_migration_here__'),
    ]

    operations = [
        migrations.RunSQL("ALTER TABLE social_auth_usersocialauth ALTER COLUMN extra_data SET DATA TYPE jsonb USING extra_data::jsonb;")
    ]

From your error you can see its a text field via the **text** -> unknown part of the message.

agconti avatar Jul 20 '22 01:07 agconti