Ecommerce Plugin Error: Duplicate Entry '1' for key 'default_site_id'

I am having difficulties enabling my ecommerce plugin.
For some point in time, we have upgraded our instance into the latest version of tutor (Olive).

However, upon upgrading, we’ve noticed that the ecommerce is disabled. So we’ve tried to enable it by enabling mfe and the discovery plugin then performing the following commands.

tutor plugins enable ecommerce
tutor config save
tutor images build openedx
tutor local launch

Upon launching, I received the following error.

Traceback.txt (6.4 KB)


You may also ask some certain questions such as the site_ids. At our https://domain/admin/sites/site/, here are the registered site_ids:

We can’t delete the id 1 since it is referenced as a foreign key on some tables.

How should I address this error?

Hi @Engr_James_Lusuegro, this error was also reported by @lpm0073 here: openedx/ecommerce-job: django.db.utils.IntegrityError: (1062, "Duplicate entry '1' for key 'default_site_id'") · Issue #25 · overhangio/tutor-ecommerce · GitHub
Actually, the error occurs because the create_or_update_site script attempts to create a new partner object that points to the same site ID:

   File "/openedx/ecommerce/ecommerce/core/management/commands/create_or_update_site.py", line 206, in handle 
     partner.save() 

What are your partner codes?

(next time, please copy-paste error logs instead of posting screenshots. It makes it much easier for us to search logs. Also, it allows other users who face the same error to find your topic.)

1 Like

Thank you for your reply. If you would look carefully at my post, I have a Traceback.txt file indicating the posted image’s error.

but I think you’re right so I’ll post it here either way

Traceback (most recent call last):
  File "/openedx/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/openedx/venv/lib/python3.8/site-packages/django/db/backends/mysql/base.py", line 73, in execute
    return self.cursor.execute(query, args)
  File "/openedx/venv/lib/python3.8/site-packages/MySQLdb/cursors.py", line 209, in execute
    res = self._query(query)
  File "/openedx/venv/lib/python3.8/site-packages/MySQLdb/cursors.py", line 315, in _query
    db.query(q)
  File "/openedx/venv/lib/python3.8/site-packages/MySQLdb/connections.py", line 239, in query
    _mysql.connection.query(self, query)
MySQLdb._exceptions.IntegrityError: (1062, "Duplicate entry '1' for key 'default_site_id'")

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

Traceback (most recent call last):
  File "./manage.py", line 11, in <module>
    execute_from_command_line(sys.argv)
  File "/openedx/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/openedx/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/openedx/venv/lib/python3.8/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/openedx/venv/lib/python3.8/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/openedx/ecommerce/ecommerce/core/management/commands/create_or_update_site.py", line 206, in handle
    partner.save()
  File "/openedx/venv/lib/python3.8/site-packages/django/db/models/base.py", line 739, in save
    self.save_base(using=using, force_insert=force_insert,
  File "/openedx/venv/lib/python3.8/site-packages/django/db/models/base.py", line 776, in save_base
    updated = self._save_table(
  File "/openedx/venv/lib/python3.8/site-packages/django/db/models/base.py", line 858, in _save_table
    updated = self._do_update(base_qs, using, pk_val, values, update_fields,
  File "/openedx/venv/lib/python3.8/site-packages/django/db/models/base.py", line 912, in _do_update
    return filtered._update(values) > 0
  File "/openedx/venv/lib/python3.8/site-packages/django/db/models/query.py", line 802, in _update
    return query.get_compiler(self.db).execute_sql(CURSOR)
  File "/openedx/venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1559, in execute_sql
    cursor = super().execute_sql(result_type)
  File "/openedx/venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1175, in execute_sql
    cursor.execute(sql, params)
  File "/openedx/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/openedx/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/openedx/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/openedx/venv/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/openedx/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/openedx/venv/lib/python3.8/site-packages/django/db/backends/mysql/base.py", line 73, in execute
    return self.cursor.execute(query, args)
  File "/openedx/venv/lib/python3.8/site-packages/MySQLdb/cursors.py", line 209, in execute
    res = self._query(query)
  File "/openedx/venv/lib/python3.8/site-packages/MySQLdb/cursors.py", line 315, in _query
    db.query(q)
  File "/openedx/venv/lib/python3.8/site-packages/MySQLdb/connections.py", line 239, in query
    _mysql.connection.query(self, query)
django.db.utils.IntegrityError: (1062, "Duplicate entry '1' for key 'default_site_id'")
ERROR: 1
Error: Command failed with status 1: docker-compose -f /home/ubuntu/.local/share/tutor/env/local/docker-compose.yml -f /home/ubuntu/.local/share/tutor/env/local/docker-compose.prod.yml -f /home/ubuntu/.local/share/tutor/env/local/docker-compose.tmp.yml --project-name tutor_local -f /home/ubuntu/.local/share/tutor/env/local/docker-compose.jobs.yml -f /home/ubuntu/.local/share/tutor/env/local/docker-compose.jobs.tmp.yml run --rm ecommerce-job sh -e -c ./manage.py migrate --noinput
./manage.py oscar_populate_countries --initial-only

./manage.py create_or_update_site \
  --site-id=1 \
  --site-domain=ecommerce.tmtg-clone.click:8130 \
  --site-name=dev \
  --partner-code=dev \
  --partner-name="Open edX - development" \
  --lms-url-root="http://tmtg-clone.click:8000" \
  --client-side-payment-processor=cybersource \
  --payment-processors=cybersource,paypal \
  --sso-client-id="ecommerce-sso-dev" \
  --sso-client-secret="CTCS23rz" \
  --backend-service-client-id="ecommerce-dev" \
  --backend-service-client-secret="eMCJ6NtJ" \
  --from-email=tmtcoredev@gmail.com \
  --payment-support-email=tmtcoredev@gmail.com \
  --payment-support-url="http://tmtg-clone.click:8000/support" \
  --discovery_api_url=http://discovery.tmtg-clone.click:8381/api/v1/ \
  --enable-microfrontend-for-basket-page=true \
  --payment-microfrontend-url="http://apps.tmtg-clone.click:1998/payment"

# Production site
./manage.py create_or_update_site \
  --site-id=2 \
  --site-domain=ecommerce.tmtg-clone.click \
  --site-name=prod \
  --partner-code=openedx \
  --partner-name="Open edX" \
  --lms-url-root="https://tmtg-clone.click" \
  --lms-public-url-root="https://tmtg-clone.click" \
  --client-side-payment-processor=cybersource \
  --payment-processors=cybersource,paypal \
  --sso-client-id="ecommerce-sso" \
  --sso-client-secret="CTCS23rz" \
  --backend-service-client-id="ecommerce" \
  --backend-service-client-secret="eMCJ6NtJ" \
  --from-email=tmtcoredev@gmail.com \
  --payment-support-email=tmtcoredev@gmail.com \
  --payment-support-url="https://tmtg-clone.click/support" \
  --discovery_api_url=https://discovery.tmtg-clone.click/api/v1/ \
  --enable-microfrontend-for-basket-page=true \
  --payment-microfrontend-url="https://apps.tmtg-clone.click/payment"

also, what are partner codes?

I faced the same problem when I was doing a migration from previous versions. If you look into the post shared by Regis, it is clearly pointing out this issue and you need to write little SQL command to migrate your partner code for existing transactions.

I did the same thing in my case and the problem is resolved.

1 Like

Upon reviewing my database, here is the django_site table.

+----+-----------------------+-----------------------+
| id | domain                | name                  |
+----+-----------------------+-----------------------+
|  1 | example.com           | example.com           |
|  2 | coursebank.ph         | coursebank.ph         |
|  3 | courses.coursebank.ph | courses.coursebank.ph |
|  4 | tmtg-clone.click      | tmtg-clone.click      |
|  5 | tmtg-clone.click:8000 | tmtg-clone.click:8000 |
+----+-----------------------+-----------------------+

Based on your instructions here, I should replace the site_ids 1 and 2. What should I replace dev and openedx with? The domain or the name?

Also what would be its effects?
Please bear with my lack of knowledge here since I’m a complete beginner in tutor. So there will be certain cases that I don’t even understand even this simple partner codes. I don’t know where to find it and I apologize in that matter.
I will be waiting for some explanation and more accurate assistance before proceeding.

Hello @regis

I’ve noticed that in the Traceback error:

Error: Command failed with status 1: docker-compose -f /home/ubuntu/.local/share/tutor/env/local/docker-compose.yml -f /home/ubuntu/.local/share/tutor/env/local/docker-compose.prod.yml -f /home/ubuntu/.local/share/tutor/env/local/docker-compose.tmp.yml --project-name tutor_local -f /home/ubuntu/.local/share/tutor/env/local/docker-compose.jobs.yml -f /home/ubuntu/.local/share/tutor/env/local/docker-compose.jobs.tmp.yml run --rm ecommerce-job sh -e -c ./manage.py migrate --noinput
./manage.py oscar_populate_countries --initial-only

./manage.py create_or_update_site \
  --site-id=1 \
  --site-domain=ecommerce.tmtg-clone.click:8130 \
  --site-name=dev \
  --partner-code=dev \
  --partner-name="Open edX - development" \
  --lms-url-root="http://tmtg-clone.click:8000" \
  --client-side-payment-processor=cybersource \
  --payment-processors=cybersource,paypal \
  --sso-client-id="ecommerce-sso-dev" \
  --sso-client-secret="CTCS23rz" \
  --backend-service-client-id="ecommerce-dev" \
  --backend-service-client-secret="eMCJ6NtJ" \
  --from-email=tmtcoredev@gmail.com \
  --payment-support-email=tmtcoredev@gmail.com \
  --payment-support-url="http://tmtg-clone.click:8000/support" \
  --discovery_api_url=http://discovery.tmtg-clone.click:8381/api/v1/ \
  --enable-microfrontend-for-basket-page=true \
  --payment-microfrontend-url="http://apps.tmtg-clone.click:1998/payment"

# Production site
./manage.py create_or_update_site \
  --site-id=2 \
  --site-domain=ecommerce.tmtg-clone.click \
  --site-name=prod \
  --partner-code=openedx \
  --partner-name="Open edX" \
  --lms-url-root="https://tmtg-clone.click" \
  --lms-public-url-root="https://tmtg-clone.click" \
  --client-side-payment-processor=cybersource \
  --payment-processors=cybersource,paypal \
  --sso-client-id="ecommerce-sso" \
  --sso-client-secret="CTCS23rz" \
  --backend-service-client-id="ecommerce" \
  --backend-service-client-secret="eMCJ6NtJ" \
  --from-email=tmtcoredev@gmail.com \
  --payment-support-email=tmtcoredev@gmail.com \
  --payment-support-url="https://tmtg-clone.click/support" \
  --discovery_api_url=https://discovery.tmtg-clone.click/api/v1/ \
  --enable-microfrontend-for-basket-page=true \
  --payment-microfrontend-url="https://apps.tmtg-clone.click/payment"

I should hard code my database by changing

site_id 1’s :
domain into ecommerce.tmtg-clone.click:8130,
partner-code into dev and
site-name into dev

while on site_id 2’s :
domain into ecommerce.tmtg-clone.click,
partner-code into openedx and
site-name into prod

How can I do it? I mean I can change the domain and name in MySQL, but there is no partner-code column on my database. Will be waiting for your replies.

DO NOT CHANGE SITE IDs!!! It will break your database.

You should only change your partner codes. To do that, you should open the ecommerce /admin page and search for partner objects there. On a local installation the url is: http://ecommerce.local.overhang.io/admin/partner/partner/ Select the right partners and set their code to either “openedx” (in production) or “dev” (in development).

1 Like

But I guess, this issue again came when he going to run quickstart again.

When I go to https://ecommerce.domain.com,

it looks like this:

and when I go to the url https://ecommerce.domain.com/admin/, my account doesn’t login on it despite being a staff user. Why is that? @regis

Also, the ecommerce plugin is not even installed in the first place so how can I access this url?

Hello @regis I’ve tried accessing the ecommerce database through mysql. I’ve realized that though it’s not installed, a partner code should be available here since we have an ecommerce before from our native instance.

mysql> select * from partner_partner;
+----+------+----------+------------+-----------------+
| id | code | name     | short_code | default_site_id |
+----+------+----------+------------+-----------------+
|  1 | edX  | Open edX | edX        |               1 |
|  2 | dev  |          |            |            NULL |
+----+------+----------+------------+-----------------+
2 rows in set (0.00 sec)

So based on here, I’ve seen the partner codes you are pertaining to. I will try to rename it through hardcode.

• Are there any things I should be considering other than this?
• and also, just to be clear, should I change id into 1 into dev and id 2 into openedx.?

I hope I am on the right track

We ran into a similar problem when we migrated from Native Koa to Tutor Koa. In fact, the problem occurred when we then migrated from Tutor Koa to Tutor Lilac.

I generally agree with @regis and I would not recommend to CHANGE SITE IDs. Unfortunately we did after a very careful reverse engineering of the ecommerce database under Native and under Tutor.

In the end, it worked for us, but I would not recommend doing it the way we did it.

It was a three steps process.

First, editing the ecommerce dump file from Koa Native to remove some tables for which we wanted to use the values generated by the default Tutor installation.

Second, restoring our new ecommerce dump in Tutor.

Third, running a script that would switch in multiple tables the values of site_id from 1 to 2 and values of partner_id from 1 to 2. That’s because our values where the reverse of what was created by Tutor in both the django_site and partner_partner tables. Our django_site for dev was 2 and for prod it was 1. The same for partner_partner. This might differ for you since you have id 4 and id 5 in your django_site table. I am guessing that id 2 and id 3 where created at some point in the past from a previous installation under Native.

We could share what we did, but we do not feel comfortable making it public because every migration could be different and the values to change could also be different.

You would need to analyze the content of your database in order for it to make sense for your own installation.