[英]django// 1054, Unknown column 'rank.post_id_id' in 'field list'"
django=2.2.3 mariadb django=2.2.3 mariadb
This error occurs after importing model from an existing database with 'inspectdb' and changed field properties.从具有“inspectdb”的现有数据库中导入 model 并更改字段属性后,会发生此错误。
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)
Originally, it was # post_id, but I removed "managed = False", changed it to ForeignKey and then "migrate".原来是#post_id,但是我去掉了“managed = False”,改成了ForeignKey,然后“迁移”了。 As far as I know that if the "post_id" in the "Post" model is "primary_key True", it replaces the "id" value with "post_id".据我所知,如果“Post”model 中的“post_id”是“primary_key True”,它会将“id”值替换为“post_id”。 But "Django" keeps calling up the "post_id_id".但是“Django”不断调用“post_id_id”。 There is no command to refer to post_id_id elsewhere.没有命令可以在别处引用 post_id_id。 If you have a solution or something that I'm missing, please give me some advice.如果您有解决方案或我缺少的东西,请给我一些建议。
-------Add more after commented by 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)
Don't call your ForeignKey post_id
.不要打电话给你的 ForeignKey post_id
。 Call it post
.称它为post
。 The underlying database field is post_id
;底层数据库字段是post_id
; Django adds the _id
suffix automatically. Django 自动添加_id
后缀。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.