0

I currently have a database using Heroku and want to migrate it to AWS, where both use PostgresSQL. So, after some digging on how to get it done I followed the steps as on this youtube video.

I initially ran python manage.py dumpdata > dumpdata.json with my Heroku database credentials in Django.

Afterwards, I changed my database credentials in settings.py to the AWS database credentials, and ran python manage.py migrate --run-syncdb which worked successfully.

And then I ran the code python manage.py loaddata dumpdata.json, when where I was thrown an error.

The following error came up:

django.db.utils.IntegrityError: Problem installing fixture 'C:\Users\Acer\Desktop\Web Development\Proffesional\eblossom\eblossom\eblossom\dumpdata.json': Could not load contenttypes.ContentType(pk=9): duplicate key value violates unique constraint "django_content_type_app_label_model_76bd3d3b_uniq"
DETAIL:  Key (app_label, model)=(user, profile) already exists.

I don't understand what has gone wrong over here, the site is working perfectly fine all this time with no database compilation error, but now when I try to migrate it to AWS, I am thrown with this problem.

Just in case my models.py:

class Profile (models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    mobile_number = models.CharField(max_length=12, null=True)
    guest = models.BooleanField(default=False)

    def __str__(self):
        return f"{self.user}"
Najaaz
  • 380
  • 5
  • 16
  • The error means that there is already a profile with that primary key in the database. Is there already a profile user in your AWS db prior to you running `loaddata`? – Scratch'N'Purr Jul 01 '21 at 06:05
  • no there isn't my database is empty – Najaaz Jul 01 '21 at 06:12
  • Disregard my earlier comment. The error is not because there is already a profile. It's actually a problem with you dumping the `contenttype` table, which contains metadata on your models. You should exclude contenttypes in your `dumpdata` command, `e.g: python manage.py dumpdata --exclude=contenttypes`. Similar thread [here](https://stackoverflow.com/a/65734649/6245650) – Scratch'N'Purr Jul 01 '21 at 06:19
  • So by running the command `python manage.py dumpdata --exclude=contenttypes > dumpdata.json`, I get a much smaller file without the contents of my database....which is a bit concerning – Najaaz Jul 01 '21 at 06:27
  • Yea, the file is going to be smaller because you're no longer dumping the contenttypes data into the json file. Have a read here on the [contenttypes framework](https://docs.djangoproject.com/en/3.2/ref/contrib/contenttypes/). – Scratch'N'Purr Jul 01 '21 at 07:37
  • editor's note: please do not add noise in posts (i.e. "thanks", etc). The only thanks posters need is an upvote iif the post is helpful and an accept checkmark if the post helped you the most. – Oleg Valter is with Ukraine Jul 07 '21 at 07:24

1 Answers1

1

I do this all the time between databases, local and remote, postgres or sqlite.

In order to achieve that, do the following steps in order :

    # Start the same as you did on local database
    python manage.py dumpdata > db.json
    # push this file to your production/server location
    # and delete or start with a fresh new database
    python manage.py migrate
    python manage.py shell 
    # Enter the following in the shell
    from django.contrib.contenttypes.models import ContentType
    ContentType.objects.all().delete()
    # Exit shell and run following command
    python manage.py loaddata db.json
noes1s
  • 178
  • 1
  • 8