Alberto Carmona
Alberto Carmona

Reputation: 477

Relation does not exist django migrations

I'm deploying a django application on an EC2 instance and have the following models.py:

class Profile(models.Model):

user = models.OneToOneField(User, on_delete=models.CASCADE)
foto = models.CharField(max_length=200, default="static/batman-for-

def __str__(self):
    return str(self.user.first_name) + " Profile"


@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)


@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()

When I login using the django user, I get the following error:

ProgrammingError at /admin/login/
relation "profiles_profile" does not exist
LINE 1: ..."."profiles_profile" FROM "profiles_...

coming from:

/home/ubuntu/chimpy/profiles/models.py in save_user_profile
instance.profile.save() 

I've looked into the postgres database and I can't find a table called Profile so I guess my migrations failed, when I run python manage.py showmigrations it shows:

 admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
auth
 [X] 0001_initial
 [X] 0002_alter_permission_name_max_length
 [X] 0003_alter_user_email_max_length
 [X] 0004_alter_user_username_opts
 [X] 0005_alter_user_last_login_null
 [X] 0006_require_contenttypes_0002
 [X] 0007_alter_validators_add_error_messages
 [X] 0008_alter_user_username_max_length
 [X] 0009_alter_user_last_name_max_length
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
sessions
 [X] 0001_initial

but nothing about the other tables (including the profile one). I also want to mention that when I create a super user in the terminal I get the following error:

django.db.utils.ProgrammingError: relation "profiles_profile" does not exist
LINE 1: INSERT INTO "profiles_profile" ("user_id", "foto", "exchange...

what am I doing wrong? Thanks.

Edit: my settings.py include the following:

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'profiles',
'portfolios',
'django_extensions',
'rest_framework',
'corsheaders',
]

Upvotes: 0

Views: 3650

Answers (1)

Henry Woody
Henry Woody

Reputation: 15662

Looking at the output of your showmigrations command, it seems the problem is that you have not created any migrations for your profiles app.

To fix this, run:

python manage.py makemigrations profiles
python manage.py migrate

Alternatively you can leave out the profiles from the above command to make migrations for all apps that require them.

Upvotes: 1

Related Questions