我是django的新手,对此在本地系统中没有问题,makemigrations,migrate和createsuperuser在本地系统中一切正常。但是当我尝试使用postgresql makemigrations在heroku中进行部署并进行迁移时,问题是当我尝试创建超级用户时,它给了我一个错误。
(djangoenv) E:\youngminds>heroku run bash
Running bash on ? youngminds... up, run.8229 (Free)
~ $ python manage.py createsuperuser
Username (leave blank to use 'u21088'): johnson
Email address: johnson@gmail.com
Password:
Password (again):
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils
.py", line 85, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedTable: relation "users_profile" does not exist
LINE 1: INSERT INTO "users_profile" ("user_id", "image", "descriptio...
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/_
_init__.py", line 371, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/_
_init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/b
ase.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/auth/mana
gement/commands/createsuperuser.py", line 59, in execute
return super().execute(*args, **options)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/b
ase.py", line 335, in execute
output = self.handle(*args, **options)
File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/auth/mana
gement/commands/createsuperuser.py", line 179, in handle
self.UserModel._default_manager.db_manager(database).create_superuser(**user
_data)
File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/auth/mode
ls.py", line 161, in create_superuser
return self._create_user(username, email, password, **extra_fields)
File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/auth/mode
ls.py", line 144, in _create_user
user.save(using=self._db)
File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/auth/base
_user.py", line 73, in save
super().save(*args, **kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py
", line 729, in save
force_update=force_update, update_fields=update_fields)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py
", line 769, in save_base
update_fields=update_fields, raw=raw, using=using,
File "/app/.heroku/python/lib/python3.6/site-packages/django/dispatch/dispatch
er.py", line 178, in send
for receiver in self._live_receivers(sender)
File "/app/.heroku/python/lib/python3.6/site-packages/django/dispatch/dispatch
er.py", line 178, in <listcomp>
for receiver in self._live_receivers(sender)
File "/app/users/signals.py", line 9, in create_profile
Profile.objects.create(user=instance) ####problem in this line
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/manager
.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.p
y", line 417, in create
obj.save(force_insert=True, using=self.db)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py
", line 729, in save
force_update=force_update, update_fields=update_fields)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py
", line 759, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, upda
te_fields)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py
", line 842, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py
", line 880, in _do_insert
using=using, raw=raw)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/manager
.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.p
y", line 1125, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/com
piler.py", line 1281, in execute_sql
cursor.execute(sql, params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils
.py", line 100, in execute
return super().execute(sql, params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils
.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._e
xecute)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils
.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils
.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", lin
e 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils
.py", line 85, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "users_profile" does not exist
LINE 1: INSERT INTO "users_profile" ("user_id", "image", "descriptio...
我没有名为users_profile的表,这为什么仅在createsuperuser命令中给我这个错误,在makemigrations和migration命令中没有问题。
我可以从错误中看到Signals.py文件中存在问题,但不知道如何导致此错误以及如何解决该错误。
signals.py
from django.db.models.signals import post_save
from django.contrib.auth.models import User
from django.dispatch import receiver
from . models import Profile
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
else:
pass
@receiver(post_save, sender=User)
def save_profile(sender, instance, **kwargs):
instance.profile.save()
heroku bash中的迁移命令。
(djangoenv) E:\youngminds>python manage.py makemigrations
Migrations for 'blog':
blog\migrations\0001_initial.py
- Create model about_page_details
- Create model carousel_images_messages
- Create model Category
- Create model Comment
- Create model No_Of_Views
- Create model Post
- Add field post to comment
- Add field reply to comment
- Add field user to comment
- Add field category to carousel_images_messages
Migrations for 'users':
users\migrations\0001_initial.py
- Create model Profile
(youngmindsenv) E:\young_minds\heroku\youngminds>python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, blog, contenttypes, sessions, social_django
, users
Running migrations:
No migrations to apply.
signals.py文件与models.py和urls.py处于同一级别
我已经回答了几乎所有有关此问题的答案,但是没有一个像这样的错误。
请对此进行帮助,为此我需要更新的任何其他信息。
预先感谢
更新
问题解决了,我要做的是解决
1)删除了我的生产表,因为我那里没有任何数据。
如克里斯在评论中建议
2)在本地运行makemigrations命令 3)直接在heroku bash中运行migration命令,而无需运行makemigrations
这样做可以解决问题!
希望这会在将来对某人有所帮助!感谢所有抽出时间对此发表评论的人