I have to tables like below:
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)
I would like to create foregin key relation between Blog and BlogCategory. Here is my command for postgres:
ALTER TABLE blog_blog ADD CONSTRAINT fk_blog_blogcategory FOREIGN KEY (category_id) REFERENCES blogcategory (name);
and i got an error:
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.