0

I am getting "no such column" whenever I try to access a foreign key. Here is a model:

class PerkLevel(models.Model):
    perk_id = models.ForeignKey('Perk', on_delete=models.CASCADE, related_name='perk_id')
    perk_level = models.IntegerField(default=1, choices=PERK_LEVELS)
    perk_desc = models.CharField(max_length=250, null=True, blank=True)

    class Meta:
        db_table = u'pl'

When I try to execute a simple query like this:

cursor.execute("SELECT perk_id FROM pl")

I get "no such column: perk_id." How can I get the foreign key when writing my own sql?

1 Answer 1

3

Django adds its own _id to the end of foreign keys. In this case, I would use perk_id_id.

Sign up to request clarification or add additional context in comments.

2 Comments

... which is one of the reasons why you shouldn't use _id in the ForeignKey name in the first place. Just call it perk, then the db column will be perk_id.
I wish Django would not mess with the name I chose. As far as I'm concerned, I did it right the first time! I stumbled upon this solution randomly after trying many other things cause I vaguely remembered reading about an '_id' in the docs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.