简体   繁体   中英

django// 1054, Unknown column 'rank.post_id_id' in 'field list'"

django=2.2.3 mariadb

This error occurs after importing model from an existing database with 'inspectdb' and changed field properties.

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". 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". But "Django" keeps calling up the "post_id_id". There is no command to refer to post_id_id elsewhere. 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 . Call it post . The underlying database field is post_id ; Django adds the _id suffix automatically.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM