[英]Create foreign key relation between tabels in postgres
我必须像下面的表格:
class BlogCategory(models.Model):
name = models.CharField(max_length=255)
class Meta:
verbose_name = 'Blog category'
verbose_name_plural = 'Blog categories'
def __unicode__(self):
return self.name
class Blog(models.Model):
category = models.ForeignKey(BlogCategory, related_name="blogs", null=True, blank=True)
我想在Blog和BlogCategory之间建立前瞻性的关键关系。 这是我对postgres的命令:
ALTER TABLE blog_blog ADD CONSTRAINT fk_blog_blogcategory FOREIGN KEY (category_id) REFERENCES blogcategory (name);
我得到一个错误:
ERROR: column "category_id" referenced in foreign key constraint does not exist
在原始命令之前运行此命令:
ALTER TABLE blog_blog ADD COLUMN category_id integer;
可以试试这个:
ALTER TABLE blog_blog ADD CONSTRAINT fk_blog_blogcategory FOREIGN KEY (name) REFERENCES blogcategory (name);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.