1

I have a problem, to solve it I tried going into the shell but I couldn't find out the solution so I hope that you can help me. I'm importing my Model Images which contains an id and the image URL but I can't get it because of this error. The code:

from recipes_database.models import Images 
Images.objects.all()

And here the error:

Traceback (most recent call last):   File "F:\Developement\Projects\WhatToCookWeb\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)   File "F:\Developement\Projects\WhatToCookWeb\venv\lib\site-packages\django\db\backends\sqlite3\base.py", line 413, in execute
    return Database.Cursor.execute(self, query, params) sqlite3.OperationalError: no such column: Images.id

The above exception was the direct cause of the following exception:

Traceback (most recent call last):   File "<console>", line 1, in <module>   File "F:\Developement\Projects\WhatToCookWeb\venv\lib\site-packages\django\db\models\query.py", line 263, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])   File "F:\Developement\Projects\WhatToCookWeb\venv\lib\site-packages\django\db\models\query.py", line 269, in __len__
    self._fetch_all()   File "F:\Developement\Projects\WhatToCookWeb\venv\lib\site-packages\django\db\models\query.py", line 1303, in _fetch_all
    self._result_cache = list(self._iterable_class(self))   File "F:\Developement\Projects\WhatToCookWeb\venv\lib\site-packages\django\db\models\query.py", line 53, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)   File "F:\Developement\Projects\WhatToCookWeb\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1154, in execute_sql
    cursor.execute(sql, params)   File "F:\Developement\Projects\WhatToCookWeb\venv\lib\site-packages\django\db\backends\utils.py", line 98, in execute
    return super().execute(sql, params)   File "F:\Developement\Projects\WhatToCookWeb\venv\lib\site-packages\django\db\backends\utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)   File "F:\Developement\Projects\WhatToCookWeb\venv\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)   File "F:\Developement\Projects\WhatToCookWeb\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)   File "F:\Developement\Projects\WhatToCookWeb\venv\lib\site-packages\django\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value   File "F:\Developement\Projects\WhatToCookWeb\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)   File "F:\Developement\Projects\WhatToCookWeb\venv\lib\site-packages\django\db\backends\sqlite3\base.py", line 413, in execute
    return Database.Cursor.execute(self, query, params) django.db.utils.OperationalError: no such column: Images.id

But when I do the same thing with my other models it works and I am wondering whats different this time. Here is my Images Model:

class Images(models.Model):
    # Field name made lowercase.
    rezept_id = models.ForeignKey(
        'Rezepte', models.DO_NOTHING, db_column='Rezept_ID', blank=True, null=True)
    # Field name made lowercase.
    image_url = models.CharField(
        db_column='Image_URL', blank=True, null=True, max_length=1000)

    class Meta:
        managed = False
        db_table = 'Images'

I think that Django somehow uses a wrong query because there is no Images.id just an Images.recipe_id. Hope you can help me Thank you!

2
  • 1
    Is recipe_id the PRIMARY KEY? What is the primary key of this table? Commented Sep 2, 2020 at 18:16
  • Thank you that was the Issue it was no primary key. Commented Sep 3, 2020 at 12:47

1 Answer 1

1

Django automatically adds a primary key if you did not specify one yourself. Indeed, as the documentation says:

By default, Django gives each model the following field:

id = models.AutoField(primary_key=True)

This is an auto-incrementing primary key.

If you’d like to specify a custom primary key, specify primary_key=True on one of your fields. If Django sees you’ve explicitly set Field.primary_key, it won’t add the automatic id column.

In your model, you did not set a primary key, so Django will automatically add an extra field id, and this will thus be used in the query as well.

For example if the image_url is the primary key, you can specify this as:

# image_url is the primary key

class Images(models.Model):
    # Field name made lowercase.
    rezept = models.ForeignKey(
        'Rezepte',
        models.DO_NOTHING,
        db_column='Rezept_ID',
        blank=True,
        null=True
    )
    # Field name made lowercase.
    image_url = models.CharField(
        db_column='Image_URL',
        blank=True,
        null=True,
        max_length=1000,
        primary_key=True
    )

    class Meta:
        managed = False
        db_table = 'Images'

In case the rezept is the primary key, it thus also means that it is unique, in that case, it makes more sense to make it a OneToOneField [Django-doc], since that is a ForeignKey with a uniqness constraint, and furthermore it has some implications on the reverse relation:

# rezept is the primary key

class Images(models.Model):
    # Field name made lowercase.
    rezept = models.OneToOneField(
        'Rezepte',
        db_column='Rezept_ID',
        primary_key=True
    )
    # Field name made lowercase.
    image_url = models.CharField(
        db_column='Image_URL',
        blank=True,
        null=True,
        max_length=1000
    )

    class Meta:
        managed = False
        db_table = 'Images'

Note: Normally one does not add a suffix _id to a ForeignKey field, since Django will automatically add a "twin" field with an _id suffix. Therefore it should be rezept, instead of rezept_id.

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

Comments

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.