1

I am deploying my Django site on to Google Cloud. One of the step is to change the database to postgresSQL. As I am using SqlLite locally I wanted to migrate all of the database into postgresSql. I followed an online guide where you Dump your data first and then change Database in settings.py to your new database. I have done everything upto this command;

    python manage.py loaddata datadump.json

where datadump.json is the dumped database from SQLITE. Now I am stuck with this error

      django.db.utils.IntegrityError: Problem installing fixtur, Could not 
    load users.Profile(pk=3): duplicate key value violates unique 
    constraint "users_profile_user_id_key" DETAIL:  Key (user_id)=(1) already exists.

and I don't have any idea as to what to do. Some answers I looked up such as this:

postgresql duplicate key violates unique constraint

AND

Django admin "duplicate key value violates unique constraint" Key (user_id)=(1) already exists

haven't helped, as I cannot understand what's going on. I did use MySQL 6 years ago, but I cannot understand this.

I managed to run some SQL commands from online resources and managed to produce this for my database:

!https://i.stack.imgur.com/Yb9ns.jpg

I followed these guides:

https://medium.com/@aaditya.chhabra/how-to-use-postgresql-with-your-django-272d59d28fa5

https://www.shubhamdipt.com/blog/django-transfer-data-from-sqlite-to-another-database/

obadul024
  • 163
  • 1
  • 1
  • 13

2 Answers2

0
  1. Drop the database
  2. create a new one and run migrations

    python manage.py migrate

And try importing data from json file using,

python manage.py loaddata datadump.json

The errors are caused by the superuser object in the database you created using 'createsuperuser' command.

  • 1
    I tried to do that, still gave the same error. and I don't have any superusers as is obvious. Here's the error; django.db.utils.IntegrityError: Problem installing fixture 'C:\Users\~\myDjangoSite\venv\my Site\datadump.json': Could not load contenttypes.ContentType(pk=1): duplicate key value violates unique constraint "django_c ontent_type_app_label_model_76bd3d3b_uniq" DETAIL: Key (app_label, model)=(admin, logentry) already exists. – obadul024 Apr 13 '19 at 21:21
  • Here's the log from >python manage.py migrate Operations to perform: Apply all migrations: admin, auth, blog, contenttypes, sessions, users Running migrations: No migrations to apply. – obadul024 Apr 13 '19 at 21:21
0

TL;DR

Just delete the unwanted record from django_site table.


Brief

I had the same problem because I used Signin with Google in my site. So I tried it 2 times to see how to setup Social Login. As a result, table django_site created 2 records. Just delete the unwanted record from that Table or Collection

ajinzrathod
  • 925
  • 10
  • 28
  • Hi, How to do it? I mean How do I know there is unwanted record in Table? – Jakhongir Alikhanov May 15 '21 at 05:54
  • If you have only used *Signin with Google*. Open your database. Now, open table named `django_site`. If there are more than 1 record, that record is the unwanted record. In my case, `id:1` was unwanted. If you have used multiple authentication like facebook and google both. Sorry, I cant help you. – ajinzrathod May 15 '21 at 08:11