Django PostgreSQL 数据库错误: 关系"categories"不存在

4

我正在开发一个Django应用程序。最初我使用MySQL作为数据库。然后,我需要在Heroku上部署一个演示应用程序,该应用程序使用PostgreSQL。

当我尝试创建一个对象时(即使是从shell中),我在Heroku上遇到了错误。

这是我尝试做的:

>> from store.models import Product, Category
>> cat = Category()
>> cat.name = 'books'
>> cat.description = 'books'
>> cat.slug = 'books'
>> cat.save()

I get the following error:

......
DatabaseError: relation "categories" does not exist

这是我的类别(Category)和产品(Product)模型:
class Category(models.Model):
  name = models.CharField(max_length=50)
  description = models.TextField()
  slug = models.SlugField(max_length=50, unique=True)
  created_at = models.DateTimeField(auto_now_add=True)
  updated_at = models.DateTimeField(auto_now=True)

class Product(models.Model):
  name = models.CharField(max_length=100, unique=True)
  description = models.TextField()
  price = models.DecimalField(max_digits=9, decimal_places=2)
  slug = models.SlugField(max_length=50, unique=True)
  categories = models.ManyToManyField(Category)
  created_at = models.DateTimeField(auto_now_add=True)
  updated_at = models.DateTimeField(auto_now=True)

它在MySQL上运行非常好,但在PostgreSQL上则不行。

有人可以帮忙吗?

谢谢。


1
非常直观。由于某些原因,该字段实际上并不存在于您的数据库表中。运行 python manage.py dbshell 然后在提示符处输入 \d+ yourapp_product 以查看当前状态是什么。 - Chris Pratt
1个回答

6

您需要在Heroku中运行syncdb,以便安装您的应用程序和模型。

heroku run python manage.py syncdb

如果您对架构进行了更改并且正在使用South,则应执行以下操作:
heroku run python manage.py schemamigration appname --auto

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接