Migration Error on updating to server v0.12.14

Hi @alex
I am updating the oppia server version from v0.12.13 to v0.12.14. When i run the “python manage.py migrate” command, the migration is failing and I am getting the Error below.

Applying oppia.0027_auto_20201206_1845…Traceback (most recent call last):
File “/mnt/c/Users/OPEN Deliver/COVID-19-Response-Uganda/oppia/env/lib/python3.6/site-packages/django/db/backends/utils.py”, line 84, in _execute
return self.cursor.execute(sql, params)
File “/mnt/c/Users/OPEN Deliver/COVID-19-Response-Uganda/oppia/env/lib/python3.6/site-packages/django/db/backends/mysql/base.py”, line 71, in execute
return self.cursor.execute(query, args)
File “/mnt/c/Users/OPEN Deliver/COVID-19-Response-Uganda/oppia/env/lib/python3.6/site-packages/MySQLdb/cursors.py”, line 209, in execute
res = self._query(query)
File “/mnt/c/Users/OPEN Deliver/COVID-19-Response-Uganda/oppia/env/lib/python3.6/site-packages/MySQLdb/cursors.py”, line 315, in _query
db.query(q)
File “/mnt/c/Users/OPEN Deliver/COVID-19-Response-Uganda/oppia/env/lib/python3.6/site-packages/MySQLdb/connections.py”, line 239, in query
_mysql.connection.query(self, query)
MySQLdb._exceptions.OperationalError: (1005, ‘Can’t create table oppianew.oppia_badge (errno: 150 “Foreign key constraint is incorrectly formed”)’)

The above exception was the direct cause of the following exception:



.
django.db.utils.OperationalError: (1005, ‘Can’t create table oppianew.oppia_badge (errno: 150 “Foreign key constraint is incorrectly formed”)’)

HI Hakim,

You might need to check that you have the default_badges.json fixture loaded, as the course completed badge is referenced in one of the recent migrations. But let me know if that doesn;t fix the issue.
Cheers,
Alex

I have loaded the default_badges.json. When I run the Migrate command, am getting a new error below.
(Please note that I am doing this on a my laptop , not the server)

Running migrations:
Applying oppia.0028_badge_award_methods…Traceback (most recent call last):
File “manage.py”, line 14, in
execute_from_command_line(sys.argv)
File “/mnt/c/Users/OPEN Deliver/COVID-19-Response-Uganda/oppia/env/lib/python3.6/site-packages/django/core/management/init.py”, line 381, in execute_from_command_line
utility.execute()
File “/mnt/c/Users/OPEN Deliver/COVID-19-Response-Uganda/oppia/env/lib/python3.6/site-packages/django/core/management/init.py”, line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File “/mnt/c/Users/OPEN Deliver/COVID-19-Response-Uganda/oppia/env/lib/python3.6/site-packages/django/core/management/base.py”, line 323, in run_from_argv
self.execute(*args, **cmd_options)
File “/mnt/c/Users/OPEN Deliver/COVID-19-Response-Uganda/oppia/env/lib/python3.6/site-packages/django/core/management/base.py”, line 364, in execute
output = self.handle(*args, **options)
File “/mnt/c/Users/OPEN Deliver/COVID-19-Response-Uganda/oppia/env/lib/python3.6/site-packages/django/core/management/base.py”, line 83, in wrapped
res = handle_func(*args, **kwargs)
File “/mnt/c/Users/OPEN Deliver/COVID-19-Response-Uganda/oppia/env/lib/python3.6/site-packages/django/core/management/commands/migrate.py”, line 234, in handle
fake_initial=fake_initial,
File “/mnt/c/Users/OPEN Deliver/COVID-19-Response-Uganda/oppia/env/lib/python3.6/site-packages/django/db/migrations/executor.py”, line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File “/mnt/c/Users/OPEN Deliver/COVID-19-Response-Uganda/oppia/env/lib/python3.6/site-packages/django/db/migrations/executor.py”, line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File “/mnt/c/Users/OPEN Deliver/COVID-19-Response-Uganda/oppia/env/lib/python3.6/site-packages/django/db/migrations/executor.py”, line 245, in apply_migration
state = migration.apply(state, schema_editor)
File “/mnt/c/Users/OPEN Deliver/COVID-19-Response-Uganda/oppia/env/lib/python3.6/site-packages/django/db/migrations/migration.py”, line 121, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File “/mnt/c/Users/OPEN Deliver/COVID-19-Response-Uganda/oppia/env/lib/python3.6/site-packages/django/db/migrations/operations/special.py”, line 190, in database_forwards
self.code(from_state.apps, schema_editor)
File “/mnt/c/Users/OPEN Deliver/COVID-19-Response-Uganda/oppia/django-oppia/oppia/migrations/0028_badge_award_methods.py”, line 42, in add_badge_methods
badge_obj = badge.objects.get(ref=‘coursecompleted’)
File “/mnt/c/Users/OPEN Deliver/COVID-19-Response-Uganda/oppia/env/lib/python3.6/site-packages/django/db/models/manager.py”, line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File “/mnt/c/Users/OPEN Deliver/COVID-19-Response-Uganda/oppia/env/lib/python3.6/site-packages/django/db/models/query.py”, line 412, in get
(self.model._meta.object_name, num)
fake.MultipleObjectsReturned: get() returned more than one Badge – it returned 2!

That seems to show that there are 2 badges with the same reference (“coursecompleted”) in the Badge model/table. I’d suggest removing one of them and running the migration again.

Given these issue you’re having with the migration, I’d suggest taking a copy of the live db and doing a test run on that - maybe you were planning to do that anyway?

Cheers,
Alex

Deleting the first entry in the ‘oppia_badge’ table from the database has solved this.

I usually test the update on the local machine first, then I move on to the stagging server, then I proceed to the live server. And I first make a copy of the directory and database of the live site before the update.
Thank you.

Thanks, good to know that worked!