0

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)
Miguel
  • 377
  • 1
  • 17

1 Answers1

2

Don't call your ForeignKey post_id. Call it post. The underlying database field is post_id; Django adds the _id suffix automatically.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Thank you for your reply. If possible, would you please take another look at my updated question? As you said, the post went well. But what should I do with field names if uid? raised "1054, Unknown column 'rank.uid_id' in 'field list'" – Miguel Sep 23 '19 at 10:51
  • I don't really understand the update. Why have you used the name `uid` for the FK to Gallery? Why not `gallery`? – Daniel Roseman Sep 23 '19 at 11:40