wger-project / docker

Production...ish docker-compose image for wger

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DETAIL: Key (language_id)=(4) is not present in table "core_language".

witchent opened this issue · comments

While trying to update ecercises (using podman exec wger python3 manage.py sync-exercises) I get the following error:

Feb 12 02:30:05 hostname sh[410879]: updated exercise ea63d85c-8579-4dda-b99f-c4c8930f9af6
Feb 12 02:30:05 hostname sh[410879]: Traceback (most recent call last):
Feb 12 02:30:05 hostname sh[410879]:   File "/usr/local/lib/python3.10/dist-packages/django/db/backends/base/bas>
Feb 12 02:30:05 hostname sh[410879]:     return self.connection.commit()
Feb 12 02:30:05 hostname sh[410879]: psycopg2.errors.ForeignKeyViolation: insert or update on table "exercises_e>
Feb 12 02:30:05 hostname sh[410879]: DETAIL:  Key (language_id)=(4) is not present in table "core_language".
Feb 12 02:30:05 hostname sh[410879]: The above exception was the direct cause of the following exception:
Feb 12 02:30:05 hostname sh[410879]: Traceback (most recent call last):
Feb 12 02:30:05 hostname sh[410879]:   File "/home/wger/src/manage.py", line 24, in <module>
Feb 12 02:30:05 hostname sh[410879]:     execute_from_command_line(sys.argv)
Feb 12 02:30:05 hostname sh[410879]:   File "/usr/local/lib/python3.10/dist-packages/django/core/management/__in>
Feb 12 02:30:05 hostname sh[410879]:     utility.execute()
Feb 12 02:30:05 hostname sh[410879]:   File "/usr/local/lib/python3.10/dist-packages/django/core/management/__in>
Feb 12 02:30:05 hostname sh[410879]:     self.fetch_command(subcommand).run_from_argv(self.argv)
Feb 12 02:30:05 hostname sh[410879]:   File "/usr/local/lib/python3.10/dist-packages/django/core/management/base>
Feb 12 02:30:05 hostname sh[410879]:     self.execute(*args, **cmd_options)
Feb 12 02:30:05 hostname sh[410879]:   File "/usr/local/lib/python3.10/dist-packages/django/core/management/base>
Feb 12 02:30:05 hostname sh[410879]:     output = self.handle(*args, **options)
Feb 12 02:30:05 hostname sh[410879]:   File "/home/wger/src/wger/exercises/management/commands/sync-exercises.py>
Feb 12 02:30:05 hostname sh[410879]:     sync_exercises(self.stdout.write, self.remote_url, self.style.SUCCESS)
Feb 12 02:30:05 hostname sh[410879]:   File "/home/wger/src/wger/exercises/sync.py", line 104, in sync_exercises
Feb 12 02:30:05 hostname sh[410879]:     translation, translation_created = Exercise.objects.update_or_create(
Feb 12 02:30:05 hostname sh[410879]:   File "/usr/local/lib/python3.10/dist-packages/django/db/models/manager.py>
Feb 12 02:30:05 hostname sh[410879]:     return getattr(self.get_queryset(), name)(*args, **kwargs)
Feb 12 02:30:05 hostname sh[410879]:   File "/usr/local/lib/python3.10/dist-packages/django/db/models/query.py",>
Feb 12 02:30:05 hostname sh[410879]:     with transaction.atomic(using=self.db):
Feb 12 02:30:05 hostname sh[410879]:   File "/usr/local/lib/python3.10/dist-packages/django/db/transaction.py", >
Feb 12 02:30:05 hostname sh[410879]:     connection.commit()
Feb 12 02:30:05 hostname sh[410879]:   File "/usr/local/lib/python3.10/dist-packages/django/utils/asyncio.py", l>
Feb 12 02:30:05 hostname sh[410879]:     return func(*args, **kwargs)
Feb 12 02:30:05 hostname sh[410879]:   File "/usr/local/lib/python3.10/dist-packages/django/db/backends/base/bas>
Feb 12 02:30:05 hostname sh[410879]:     self._commit()
Feb 12 02:30:05 hostname sh[410879]:   File "/usr/local/lib/python3.10/dist-packages/django/db/backends/base/bas>
Feb 12 02:30:05 hostname sh[410879]:     with debug_transaction(self, "COMMIT"), self.wrap_database_errors:
Feb 12 02:30:05 hostname sh[410879]:   File "/usr/local/lib/python3.10/dist-packages/django/db/utils.py", line 9>
Feb 12 02:30:05 hostname sh[410879]:     raise dj_exc_value.with_traceback(traceback) from exc_value
Feb 12 02:30:05 hostname sh[410879]:   File "/usr/local/lib/python3.10/dist-packages/django/db/backends/base/bas>
Feb 12 02:30:05 hostname sh[410879]:     return self.connection.commit()
Feb 12 02:30:05 hostname sh[410879]: django.db.utils.IntegrityError: insert or update on table "exercises_exerci>
Feb 12 02:30:05 hostname sh[410879]: DETAIL:  Key (language_id)=(4) is not present in table "core_language".

Any idea what might be happening here or how to fix this?

Sorry for the late reply! Is this on a new install? I have seen something similar happen when for some reason the database service is not ready when the application tries to bootstrap itself. I would recommend deleting the db volume, starting the service first manually and then the rest (this would only be necessary the very first time, not later)

Oh I somehow missed the notification for this error.
I cannot reproduce it right now so I'm closing this. Thanks for your answer

I am getting a similar error with another language (id=16)

I run: docker compose exec web python3 manage.py sync-exercises

it does all the following steps with no problems:

*** Synchronizing languages...
done!

*** Synchronizing categories...
done!

*** Synchronizing muscles...
done!

*** Synchronizing equipment...
done!

*** Synchronizing licenses...
done!

And when it starts Synchronizing exercises.., after updating a few:

django.db.utils.IntegrityError: insert or update on table "exercises_exercise" violates foreign key constraint "exercises_exercise_language_id_9675c687_fk_core_language_id"
DETAIL:  Key (language_id)=(16) is not present in table "core_language".

image

It indeed does not exist in the DB:

docker exec -ti wger_db psql -U wger
#  select * from core_language;

image

$ docker compose exec web python3 manage.py sync-exercises --version
INFO 2024-05-14 21:20:52,440 apps AXES: BEGIN version 6.4.0, blocking by ip_address
4.2.6
commented

Maybe check for faulty exercises:

python3 manage.py exercises-health-check

Thanks for the answer @bbkz , I did and there were 2 exercises with no translation deleted them with the script option and same problem.

I tried to add manually a language with the missing id (16) and it worked (it inserted the exercise) but then failed with the same error complaining about another missing language (id 7 I believe but not sure, I don't have the log).

after the language sync, should the table shown in my above message be complete? or is it normal to have missing ids?

commented

Hi, I don't know I never had the time to have a deep look into the code.

I remember having the same problem once, but don't remember the cause of it.

Did all your Django database migration run. manage.py migrate

ohh I know what's happening here. When we sync the new languages, we simply create new entries in the db if the short name (fr, es, etc.) is not known. If for some reason you had some and deleted them, added new ones or something, the new languages have different IDs that what the API returns, that's why the IDs have gaps in the screenshot you posted.

The solution would then be to map the language IDs we get from the server to the local ones (forcing the IDs would break local instances where the admin had manually added new ones)...

As a fix for your specific problem, running python manage.py loaddata languages should overwrite everything with the correct data (including IDs)

Yes. It must be that, I had removed languages from the interface. The quick-fix using the loaddata languages doesn't work, I guess I would need to empty the table first:

psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "core_language_short_name_935deaad_uniq"
DETAIL:  Key (short_name)=(bg) already exists.
[...]
django.db.utils.IntegrityError: Problem installing fixture '/home/wger/src/wger/core/fixtures/languages.json': Could not load core.Language(pk=3): duplicate key value violates unique constraint "core_language_short_name_935deaad_uniq"
DETAIL:  Key (short_name)=(bg) already exists.

Regarding the permanent fix mapping the language IDs I could try a PR if you're interested.

yes, empty them first and then do the loaddata. But this is still not a good solution since postgres will still insert new entries at higher IDs, so you'd potentially get problems in the future, you'd also need to reset the sequence in the db.

I have created an issue for the sync problem wger-project/wger#1667 If you want to give it a try go ahead! (and feel free to ping me to discuss stuff)