Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IntegrityError at /accounts/complete/google-oauth2/ #2074

Closed
tnextday opened this issue Jun 4, 2018 · 17 comments
Closed

IntegrityError at /accounts/complete/google-oauth2/ #2074

tnextday opened this issue Jun 4, 2018 · 17 comments
Assignees
Labels
bug Something is broken.
Milestone

Comments

@tnextday
Copy link

tnextday commented Jun 4, 2018

Steps to reproduce

  1. Upgrade weblate from 2.20 to 3.0-1 (docker container)
  2. Register new user with a google account (or email)

Actual behaviour

Server Error
error log:

Internal Server Error: /accounts/complete/google-oauth2/

IntegrityError at /accounts/complete/google-oauth2/
insert or update on table "authtoken_token" violates foreign key constraint "authtoken_token_user_id_35299eff_fk_auth_user_id"
DETAIL:  Key (user_id)=(8255) is not present in table "auth_user".


Request Method: GET
Request URL: https://xxx.com/accounts/complete/google-oauth2/?state=...
Django Version: 2.0.6
Python Executable: /usr/bin/uwsgi-core
Python Version: 3.5.3
Python Path: ['/usr/local/lib/python3.5/dist-packages/', '.', '', '/usr/lib/python35.zip', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-x86_64-linux-gnu', '/usr/lib/python3.5/lib-dynload', '/usr/local/lib/python3.5/dist-packages', '/usr/lib/python3/dist-packages']
Server time: Mon, 4 Jun 2018 07:58:00 +0000
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin.apps.SimpleAdminConfig',
'django.contrib.admindocs',
'django.contrib.sitemaps',
'social_django',
'crispy_forms',
'compressor',
'rest_framework',
'rest_framework.authtoken',
'weblate.addons',
'weblate.auth',
'weblate.checks',
'weblate.formats',
'weblate.machinery',
'weblate.trans',
'weblate.lang',
'weblate.langdata',
'weblate.memory',
'weblate.permissions',
'weblate.screenshots',
'weblate.accounts',
'weblate.utils',
'weblate.vcs',
'weblate.wladmin',
'weblate',
'weblate.gitexport')
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'weblate.accounts.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'social_django.middleware.SocialAuthExceptionMiddleware',
'weblate.accounts.middleware.RequireLoginMiddleware',
'weblate.middleware.SecurityMiddleware',
'weblate.wladmin.middleware.ConfigurationErrorsMiddleware']


Traceback:

File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/base.py" in _commit
 239.                 return self.connection.commit()

The above exception (insert or update on table "authtoken_token" violates foreign key constraint "authtoken_token_user_id_35299eff_fk_auth_user_id"
DETAIL:  Key (user_id)=(8255) is not present in table "auth_user".
) was the direct cause of the following exception:

File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py" in inner
 35.             response = get_response(request)

File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py" in _get_response
 128.                 response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py" in _get_response
 126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/local/lib/python3.5/dist-packages/django/views/decorators/csrf.py" in wrapped_view
 54.         return view_func(*args, **kwargs)

File "/usr/local/lib/python3.5/dist-packages/weblate/accounts/views.py" in demo_wrap
 193.         return function(request, *args, **kwargs)

File "/usr/local/lib/python3.5/dist-packages/django/views/decorators/cache.py" in _wrapped_view_func
 44.         response = view_func(request, *args, **kwargs)

File "/usr/local/lib/python3.5/dist-packages/weblate/accounts/views.py" in social_complete
 916.         return complete(request, backend)

File "/usr/local/lib/python3.5/dist-packages/django/views/decorators/cache.py" in _wrapped_view_func
 44.         response = view_func(request, *args, **kwargs)

File "/usr/local/lib/python3.5/dist-packages/django/views/decorators/csrf.py" in wrapped_view
 54.         return view_func(*args, **kwargs)

File "/usr/local/lib/python3.5/dist-packages/social_django/utils.py" in wrapper
 49.             return func(request, backend, *args, **kwargs)

File "/usr/local/lib/python3.5/dist-packages/social_django/views.py" in complete
 33.                        *args, **kwargs)

File "/usr/local/lib/python3.5/dist-packages/social_core/actions.py" in do_complete
 41.         user = backend.complete(user=user, *args, **kwargs)

File "/usr/local/lib/python3.5/dist-packages/social_core/backends/base.py" in complete
 40.         return self.auth_complete(*args, **kwargs)

File "/usr/local/lib/python3.5/dist-packages/social_core/utils.py" in wrapper
 252.             return func(*args, **kwargs)

File "/usr/local/lib/python3.5/dist-packages/social_core/backends/oauth.py" in auth_complete
 405.                             *args, **kwargs)

File "/usr/local/lib/python3.5/dist-packages/social_core/utils.py" in wrapper
 252.             return func(*args, **kwargs)

File "/usr/local/lib/python3.5/dist-packages/social_core/backends/oauth.py" in do_auth
 416.         return self.strategy.authenticate(*args, **kwargs)

File "/usr/local/lib/python3.5/dist-packages/social_django/strategy.py" in authenticate
 107.         return authenticate(*args, **kwargs)

File "/usr/local/lib/python3.5/dist-packages/django/contrib/auth/__init__.py" in authenticate
 70.             user = _authenticate_with_backend(backend, backend_path, request, credentials)

File "/usr/local/lib/python3.5/dist-packages/django/contrib/auth/__init__.py" in _authenticate_with_backend
 116.     return backend.authenticate(*args, **credentials)

File "/usr/local/lib/python3.5/dist-packages/social_core/backends/base.py" in authenticate
 80.         return self.pipeline(pipeline, *args, **kwargs)

File "/usr/local/lib/python3.5/dist-packages/social_core/backends/base.py" in pipeline
 83.         out = self.run_pipeline(pipeline, pipeline_index, *args, **kwargs)

File "/usr/local/lib/python3.5/dist-packages/social_core/backends/base.py" in run_pipeline
 113.             result = func(*args, **out) or {}

File "/usr/local/lib/python3.5/dist-packages/social_core/pipeline/user.py" in create_user
 75.         'user': strategy.create_user(**fields)

File "/usr/local/lib/python3.5/dist-packages/social_core/strategy.py" in create_user
 53.         return self.storage.user.create_user(*args, **kwargs)

File "/usr/local/lib/python3.5/dist-packages/social_django/storage.py" in create_user
 83.                 six.reraise(*exc_info)

File "/usr/lib/python3/dist-packages/six.py" in reraise
 686.         raise value

File "/usr/local/lib/python3.5/dist-packages/social_django/storage.py" in create_user
 70.                     user = cls.user_model().objects.create_user(*args, **kwargs)

File "/usr/local/lib/python3.5/dist-packages/django/db/transaction.py" in __exit__
 212.                         connection.commit()

File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/base.py" in commit
 261.         self._commit()

File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/base.py" in _commit
 239.                 return self.connection.commit()

File "/usr/local/lib/python3.5/dist-packages/django/db/utils.py" in __exit__
 89.                 raise dj_exc_value.with_traceback(traceback) from exc_value

File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/base.py" in _commit
 239.                 return self.connection.commit()

Exception Type: IntegrityError at /accounts/complete/google-oauth2/
Exception Value: insert or update on table "authtoken_token" violates foreign key constraint "authtoken_token_user_id_35299eff_fk_auth_user_id"
DETAIL:  Key (user_id)=(8255) is not present in table "auth_user".

Server configuration

  • Weblate 3.0
  • Python 3.5.3
  • Django 2.0.6
  • six 1.10.0
  • social-auth-core 1.7.0
  • social-auth-app-django 2.1.0
  • django-appconf 1.0.2
  • Translate Toolkit 2.3.0
  • Whoosh 2.7.4
  • defusedxml 0.5.0
  • Git 2.11.0
  • Pillow (PIL) 1.1.7
  • dateutil 2.5.3
  • lxml 3.7.1
  • django-crispy-forms 1.7.2
  • compressor 2.2
  • djangorestframework 3.8.2
  • user-agents 1.1.0
  • pytz 2018.4
  • pyuca N/A
  • pyLibravatar N/A
  • PyYAML 3.12
  • tesserocr 2.2.2
  • Mercurial 4.0
  • git-svn 2.11.0
  • hub 2.2.9
  • Database backends: django.db.backends.postgresql_psycopg2
  • Cache backends: avatar:FileBasedCache, default:MemcachedCache
@nijel
Copy link
Member

nijel commented Jun 7, 2018

It looks like the migration to 3.0 didn't really work well. Can you try dumping table structure for the authtoken_token table?

@agaida
Copy link

agaida commented Jun 7, 2018

bitten by nearly the same if i try to create a new user via weblate admin, createing a user via webinterface seems to be ok - as long no one hit the activation mail - weblate upgraded from 2.20 to 3

@tnextday
Copy link
Author

tnextday commented Jun 8, 2018

table structure

weblate=> \d authtoken_token
         Table "public.authtoken_token"
 Column  |           Type           | Modifiers
---------+--------------------------+-----------
 key     | character varying(40)    | not null
 created | timestamp with time zone | not null
 user_id | integer                  | not null
Indexes:
    "authtoken_token_pkey" PRIMARY KEY, btree (key)
    "authtoken_token_user_id_key" UNIQUE CONSTRAINT, btree (user_id)
    "authtoken_token_key_10f0b77e_like" btree (key varchar_pattern_ops)
Foreign-key constraints:
    "authtoken_token_user_id_35299eff_fk_auth_user_id" FOREIGN KEY (user_id) REFERENCES auth_user(id) DEFERRABLE INITIALLY DEFERRED

@tnextday
Copy link
Author

tnextday commented Jun 8, 2018

@nijel
Copy link
Member

nijel commented Jun 8, 2018

That's swappable model and the database migration should have been performed here: https://github.com/WeblateOrg/weblate/blob/master/weblate/auth/migrations/0007_auto_20180509_0739.py

So far it worked well in all my installations, so I'm really curious what might have gone wrong here.

Can you check if there is other foreign key on to the auth_user(id)?

@tnextday
Copy link
Author

tnextday commented Jun 8, 2018

So many ...

weblate=> \d auth_user
                                     Table "public.auth_user"
    Column    |           Type           |                       Modifiers

--------------+--------------------------+--------------------------------------
------------------
 id           | integer                  | not null default nextval('auth_user_i
d_seq'::regclass)
 password     | character varying(128)   | not null
 last_login   | timestamp with time zone |
 is_superuser | boolean                  | not null
 username     | character varying(150)   | not null
 first_name   | character varying(30)    | not null
 last_name    | character varying(30)    | not null
 email        | character varying(254)   | not null
 is_staff     | boolean                  | not null
 is_active    | boolean                  | not null
 date_joined  | timestamp with time zone | not null
Indexes:
    "auth_user_pkey" PRIMARY KEY, btree (id)
    "auth_user_username_key" UNIQUE CONSTRAINT, btree (username)
    "auth_user_username_6821ab7c_like" btree (username varchar_pattern_ops)
Referenced by:
    TABLE "accounts_auditlog" CONSTRAINT "accounts_auditlog_user_id_1dfbca79_fk_
auth_user_id" FOREIGN KEY (user_id) REFERENCES auth_user(id) DEFERRABLE INITIALL
Y DEFERRED
    TABLE "accounts_profile" CONSTRAINT "accounts_profile_user_id_49a85d32_fk_au
th_user_id" FOREIGN KEY (user_id) REFERENCES auth_user(id) DEFERRABLE INITIALLY
DEFERRED
    TABLE "auth_user_groups" CONSTRAINT "auth_user_groups_user_id_6a12ed8b_fk_au
th_user_id" FOREIGN KEY (user_id) REFERENCES auth_user(id) DEFERRABLE INITIALLY
DEFERRED
    TABLE "auth_user_user_permissions" CONSTRAINT "auth_user_user_permissions_us
er_id_a95ead1b_fk_auth_user_id" FOREIGN KEY (user_id) REFERENCES auth_user(id) D
EFERRABLE INITIALLY DEFERRED
    TABLE "authtoken_token" CONSTRAINT "authtoken_token_user_id_35299eff_fk_auth
_user_id" FOREIGN KEY (user_id) REFERENCES auth_user(id) DEFERRABLE INITIALLY DE
FERRED
    TABLE "django_admin_log" CONSTRAINT "django_admin_log_user_id_c564eba6_fk_au
th_user_id" FOREIGN KEY (user_id) REFERENCES auth_user(id) DEFERRABLE INITIALLY
DEFERRED
    TABLE "social_auth_usersocialauth" CONSTRAINT "social_auth_usersocialauth_us
er_id_17d28448_fk_auth_user_id" FOREIGN KEY (user_id) REFERENCES auth_user(id) D
EFERRABLE INITIALLY DEFERRED
    TABLE "trans_change" CONSTRAINT "trans_change_author_id_09329d48_fk_auth_use
r_id" FOREIGN KEY (author_id) REFERENCES auth_user(id) DEFERRABLE INITIALLY DEFE
RRED
    TABLE "trans_change" CONSTRAINT "trans_change_user_id_8d1225f9_fk_auth_user_
id" FOREIGN KEY (user_id) REFERENCES auth_user(id) DEFERRABLE INITIALLY DEFERRED
    TABLE "trans_comment" CONSTRAINT "trans_comment_user_id_c2f200eb_fk_auth_use
r_id" FOREIGN KEY (user_id) REFERENCES auth_user(id) DEFERRABLE INITIALLY DEFERR
ED
    TABLE "trans_suggestion" CONSTRAINT "trans_suggestion_user_id_69ce0c2b_fk_au
th_user_id" FOREIGN KEY (user_id) REFERENCES auth_user(id) DEFERRABLE INITIALLY
DEFERRED
    TABLE "trans_vote" CONSTRAINT "trans_vote_user_id_86a644fd_fk_auth_user_id"
FOREIGN KEY (user_id) REFERENCES auth_user(id) DEFERRABLE INITIALLY DEFERRED

@tnextday
Copy link
Author

tnextday commented Jun 8, 2018

And weblate_auth_user:

weblate=> \d weblate_auth_user
                                     Table "public.weblate_auth_user"
    Column    |           Type           |                           Modifiers

--------------+--------------------------+--------------------------------------
--------------------------
 id           | integer                  | not null default nextval('weblate_aut
h_user_id_seq'::regclass)
 password     | character varying(128)   | not null
 last_login   | timestamp with time zone |
 username     | character varying(150)   | not null
 full_name    | character varying(150)   | not null
 email        | character varying(190)   | not null
 is_superuser | boolean                  | not null
 is_active    | boolean                  | not null
 date_joined  | timestamp with time zone | not null
Indexes:
    "weblate_auth_user_pkey" PRIMARY KEY, btree (id)
    "weblate_auth_user_email_aaabd769_uniq" UNIQUE CONSTRAINT, btree (email)
    "weblate_auth_user_username_key" UNIQUE CONSTRAINT, btree (username)
    "weblate_auth_user_email_aaabd769_like" btree (email varchar_pattern_ops)
    "weblate_auth_user_username_d226e4a0_like" btree (username varchar_pattern_o
ps)
Referenced by:
    TABLE "trans_contributoragreement" CONSTRAINT "trans_contributoragr_user_id_
220dedfc_fk_weblate_a" FOREIGN KEY (user_id) REFERENCES weblate_auth_user(id) DE
FERRABLE INITIALLY DEFERRED
    TABLE "weblate_auth_user_groups" CONSTRAINT "weblate_auth_user_gr_user_id_37
e61ff1_fk_weblate_a" FOREIGN KEY (user_id) REFERENCES weblate_auth_user(id) DEFE
RRABLE INITIALLY DEFERRED

@tnextday
Copy link
Author

tnextday commented Jun 8, 2018

I just did another test.

  1. Create a new weblate using the weblate/weblate:2.20-1 docker image.
  2. Stop the container.
  3. Run weblate using the weblate/weblate:3.0-1 docker image.

No error in log when running migrations, However, the same error occurs when registering a user.

@tnextday
Copy link
Author

tnextday commented Jun 8, 2018

Here is the test log

Postgres is up
Operations to perform:
  Target specific migration: 0001_initial, from weblate_auth
Running migrations:
  Applying weblate_auth.0001_initial... OK
Operations to perform:
  Apply all migrations: accounts, addons, admin, auth, authtoken, checks, contenttypes, gitexport, lang, memory, permissions, screenshots, sessions, sites, social_django, trans, weblate_auth, wladmin
Running migrations:
  Applying accounts.0037_auto_20180416_1406... OK
  Applying addons.0006_addon_project_scope... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying trans.0129_auto_20180416_1541... OK
  Applying trans.0130_auto_20180416_1503... OK
  Applying checks.0001_initial... OK
  Applying checks.0002_auto_20180416_1509... OK
  Applying trans.0131_auto_20180416_1610... OK
  Applying gitexport.0002_fill_in_url... OK
  Applying weblate_auth.0002_auto_20180507_1540... OK
  Applying weblate_auth.0003_permissions... OK
  Applying weblate_auth.0004_auto_20180508_1032... OK
  Applying permissions.0009_auto_20180416_1610... OK
  Applying weblate_auth.0005_groups... OK
  Applying permissions.0010_auto_20180508_1210... OK
  Applying permissions.0011_auto_20180508_1548... OK
  Applying trans.0132_auto_20180517_1413... OK
  Applying trans.0133_auto_20180517_1425... OK
  Applying trans.0134_auto_20180517_1624... OK
  Applying trans.0135_auto_20180518_1149... OK
  Applying trans.0136_auto_20180521_1048... OK
  Applying trans.0137_commit_formatting... OK
  Applying trans.0138_auto_20180521_1116... OK
  Applying trans.0139_source_context... OK
  Applying trans.0140_change_project... OK
  Applying trans.0141_component_linked_component... OK
  Applying trans.0142_linked_component_data... OK
  Applying weblate_auth.0006_autogroup... OK
  Applying weblate_auth.0007_auto_20180509_0739... OK
  Applying weblate_auth.0008_email_fixup... OK
  Applying weblate_auth.0009_auto_20180509_1552... OK
  Applying weblate_auth.0010_auto_20180509_1630... OK

0 static files copied to '/app/data/static', 188 unmodified.

@nijel
Copy link
Member

nijel commented Jun 8, 2018

We need to figure out why weblate_auth.0007_auto_20180509_0739 is not doing it's job in this case.

@nijel nijel added the bug Something is broken. label Jun 9, 2018
@nijel nijel closed this as completed in 8a258c8 Jun 9, 2018
@nijel nijel self-assigned this Jun 9, 2018
@nijel nijel added this to the 3.1 milestone Jun 9, 2018
@nijel
Copy link
Member

nijel commented Jun 9, 2018

I think I've just fixed the issue. At least it works in screnario I was able to create to trigger such bug. It would be great if somebody else can confirm the fix (you need to start again from 2.20 to test this). Once the test is confirmed, it might make sense to create additional migration to cover broken 3.0 installs (though it still might be better for them to roll back to 2.20 and do the clean migration)....

  • In case you see problem with the fix, please comment on this issue.
  • In case you see similar problem, please open separate issue.
  • If you are happy with the outcome, consider supporting Weblate by donating.

@tnextday
Copy link
Author

tnextday commented Jun 9, 2018

I rolled back to 2.20 and then upgraded to 3.0. The problem has been solved. Thank you for making such an excellent application.

@nijel
Copy link
Member

nijel commented Jun 9, 2018

Great, thanks for providing feedback!

nijel added a commit that referenced this issue Jun 9, 2018
…lations

The 0007_auto_20180509_0739.py as shipped in 3.0 failed to migrate User
relations in some cases. This was fixed in
8a258c8, but we need to apply fix for
users which have already migrated.

Issue #2074

Signed-off-by: Michal Čihař <michal@cihar.com>
@Flohack74
Copy link

A have now the exactly same problem as we are gradually upgrading our old instance. I would prefer to fix it manually over to having to rollback again. And then a question, was this never updated in the pip sources?

@Flohack74
Copy link

Disregard, I applied update to 3.0.1 and it seems to work => Could you update the Upgrade from 2.x part so that it points directly to 3.0.1 ? That would be great.

@nijel
Copy link
Member

nijel commented Oct 15, 2018

@Flohack74
Copy link

OK somehow I managed to land on the upgrade to 3.0 page :) - no worries, everything is fine now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is broken.
Projects
None yet
Development

No branches or pull requests

4 participants