Django 1.9 - 在Heroku上makemigrations / migrate不会创建关系

时间:2016-08-23 17:32:20

标签: django heroku django-migrations

我正在尝试将我的1.9 Django项目部署到Heroku,但是在应用我的迁移时遇到了问题。在运行'python manage.py migrate'之后,所有表都是为一个用于多对多关系的关系创建的。它的定义如下:

class CardInJar(models.Model):
   card = models.ForeignKey(ContentCard)
   jar = models.ForeignKey(MessageJar)
   seen = models.BooleanField(default=False)

   class Meta:
      auto_created = True

我已经尝试在本地进行迁移并提交它们,并在服务器上进行迁移,在两者之间重置数据库。迁移后,将创建所有其他表并且这些表正常工作。但是,当我尝试使用CardInJar关系时,出现relation "YourShoeBox_cardinjar" does not exist错误。本地应用程序运行良好。

修改

因为有问题,完整的追溯:

File "/app/.heroku/python/lib/python3.5/site-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)

The above exception (relation "YourShoeBox_cardinjar" does not exist
LINE 1: ...ar"."jar_id", "YourShoeBox_cardinjar"."seen" FROM "YourShoeB...
                                                             ^
) was the direct cause of the following exception:

File "/app/.heroku/python/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)

File "/app/.heroku/python/lib/python3.5/site-packages/django/core/handlers/base.py" in _legacy_get_response
  249.             response = self._get_response(request)

File "/app/.heroku/python/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/app/.heroku/python/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/app/.heroku/python/lib/python3.5/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "/app/.heroku/python/lib/python3.5/site-packages/django/contrib/auth/mixins.py" in dispatch
  92.         return super(PermissionRequiredMixin, self).dispatch(request, *args, **kwargs)

File "/app/.heroku/python/lib/python3.5/site-packages/django/views/generic/base.py" in dispatch
  88.         return handler(request, *args, **kwargs)

File "/app/.heroku/python/lib/python3.5/site-packages/django/views/generic/detail.py" in get
  116.         context = self.get_context_data(object=self.object)

File "/app/src/YourShoeBox/views.py" in get_context_data
  158.         context['random_unseen_card'] = get_random_unseen(context["object"])

File "/app/src/YourShoeBox/views.py" in get_random_unseen
  133.     for message in jar.cardinjar_set.all():

File "/app/.heroku/python/lib/python3.5/site-packages/django/db/models/query.py" in __iter__
  256.         self._fetch_all()

File "/app/.heroku/python/lib/python3.5/site-packages/django/db/models/query.py" in _fetch_all
  1085.             self._result_cache = list(self.iterator())

File "/app/.heroku/python/lib/python3.5/site-packages/django/db/models/query.py" in __iter__
  54.         results = compiler.execute_sql()

File "/app/.heroku/python/lib/python3.5/site-packages/django/db/models/sql/compiler.py" in execute_sql
  835.             cursor.execute(sql, params)

File "/app/.heroku/python/lib/python3.5/site-packages/django/db/backends/utils.py" in execute
  79.             return super(CursorDebugWrapper, self).execute(sql, params)

File "/app/.heroku/python/lib/python3.5/site-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)

File "/app/.heroku/python/lib/python3.5/site-packages/django/db/utils.py" in __exit__
  94.                 six.reraise(dj_exc_type, dj_exc_value, traceback)

File "/app/.heroku/python/lib/python3.5/site-packages/django/utils/six.py" in reraise
  685.             raise value.with_traceback(tb)

File "/app/.heroku/python/lib/python3.5/site-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)

Exception Type: ProgrammingError at /jar/1/
Exception Value: relation "YourShoeBox_cardinjar" does not exist
LINE 1: ...ar"."jar_id", "YourShoeBox_cardinjar"."seen" FROM "YourShoeB..

models.py:

from django.contrib.auth.models import User
from django.db import models
from model_utils.managers import InheritanceManager


# Create your models here.
class ContentCard(models.Model):
    title = models.CharField(max_length=100)
    message = models.TextField()
    objects = InheritanceManager()
    from_user = models.ForeignKey(User, default=1, related_name="creator")

class ImageCard(ContentCard):
    image_url = models.CharField(max_length=10000, blank=True)

    def render(self):
        return self.title, self.message


class TextCard(ContentCard):
    mood = models.CharField(max_length=100)
    tag = models.CharField(max_length=100, default=" ")


class VideoCard(ContentCard):
    video_url = models.CharField(max_length=10000, blank=True)


class MessageJar(models.Model):
    name = models.CharField(max_length=100)
    messages = models.ManyToManyField(ContentCard, through="CardInJar", blank=True)
    to_user = models.ForeignKey(User, default=1, related_name="to_user")
    from_user = models.ForeignKey(User, default=1, related_name="from_user")

    def __str__(self):
        return self.name + " | to: " + str(self.to_user)



class CardInJar(models.Model):
    card = models.ForeignKey(ContentCard)
    jar = models.ForeignKey(MessageJar)
    seen = models.BooleanField(default=False)

    class Meta:
        auto_created = True

0 个答案:

没有答案