database - 在 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;
推荐阅读
- mysql - 放入并行管道时未在 DAG 中显示的任务
- python - 从包含公司列表的股票数据的字典中创建多个熊猫数据框
- python - Python 3 中的 @property 和 @staticmethod
- flutter - 使用 BlocBuilder 的 StatefulWidget 不会在状态更改时重建
- javascript - 我如何减少 expo build ios 大小,大约是 188 MB,
- python - Python中的散列
- php - 如何限制从 PHP while 循环创建的表的列数
- opengl - OpenGL片段着色器手动深度测试崩溃
- swing - 如何在 Apache Karaf 下部署 Java Web Start?
- django - 如何在视图中散列密码以在自定义用户模型中注册用户