关系不存在 django 迁移



我正在EC2实例上部署一个django应用程序,并具有以下 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()

当我使用 django 用户登录时,出现以下错误:

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

来自:

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

我已经查看了 postgres 数据库,但我找不到一个名为 Profile 的表,所以我想我的迁移失败了,当我运行 python manage.py 显示迁移时,它显示:

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

但其他表(包括配置文件表(没有任何内容。我还想提一下,当我在终端中创建超级用户时,出现以下错误:

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

我做错了什么?谢谢。

编辑:我的 settings.py 包括以下内容:

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',
]

查看showmigrations命令的输出,问题似乎在于您尚未为profiles应用程序创建任何迁移。

要解决此问题,请运行:

python manage.py makemigrations profiles
python manage.py migrate

或者,您可以省略上述命令中的profiles,以便为需要它们的所有应用进行迁移。

最新更新