django // 1054,“字段列表”中的未知列“ rank.post_id_id””

时间:2019-09-23 10:21:25

标签: python django inspectdb

django = 2.2.3 mariadb

从具有'inspectdb'的现有数据库中导入模型并更改字段属性后,会发生此错误。

class Post(models.Model):
    post_id = models.AutoField(primary_key=True)
    post_name = models.CharField(max_length=255, blank=True, null=True)
    email = models.CharField(max_length=255, blank=True, null=True)

class Rank(models.Model):
    rank_id = models.AutoField(primary_key=True)
    rank_type = models.IntegerField(blank=True, null=True)
    created_at = models.DateTimeField()
    # post_id = models.IntegerField(blank=True, null=True)
    post_id = models.ForeignKey(Post, on_delete=models.SET_NULL, blank=True, null=True)

最初,它是#post_id,但是我删除了“ managed = False”,将其更改为ForeignKey,然后“迁移”。 据我所知,如果“ Post”模型中的“ post_id”为“ primary_key True”,它将用“ post_id”替换“ id”值。但是“ Django”一直在调用“ post_id_id”。 在其他地方没有命令可以引用post_id_id。 如果您有解决方案或我缺少的东西,请给我一些建议。

-------在Daniel Roseman评论后添加更多--------

class Post(models.Model):
    post_id = models.AutoField(primary_key=True)
    post_name = models.CharField(max_length=255, blank=True, null=True)
    email = models.CharField(max_length=255, blank=True, null=True)

class Gallery(models.Model):
    uid = models.AutoField(prymary_key=True)
    gallery_name = models.CharField(...)


class Rank(models.Model):
    rank_id = models.AutoField(primary_key=True)
    rank_type = models.IntegerField(blank=True, null=True)
    created_at = models.DateTimeField()
    # post_id = models.IntegerField(blank=True, null=True)
    post_id = models.ForeignKey(Post, on_delete=models.SET_NULL, blank=True, null=True)
    # uid = models.IntegerField(blank=True, null=True)
    uid = models.ForeignKey(Gallery, on_delete=models.SET_NULL, blank=True, null=True)

1 个答案:

答案 0 :(得分:0)

请勿致电您的ForeignKey post_id。称为post。基础数据库字段为post_id; Django自动添加_id后缀。