0

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”,它会将“id”值替换为“post_id”。但是“Django”不断调用“post_id_id”。没有命令可以在别处引用 post_id_id。如果您有解决方案或我缺少的东西,请给我一些建议。

--------在丹尼尔罗斯曼评论后添加更多内容--------

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)
4

1 回答 1

0

不要打电话给你的 ForeignKey post_id。调用它post。基础数据库字段是post_id; Django_id自动添加后缀。

于 2019-09-23T10:29:29.263 回答