1
votes

When running the following command via the Django shell:

from myApp.models import *
print Categorytree.objects.all()

I receive this error message:

OperationalError: (1054, "Unknown column 'categorytree.id' in 'field list'")

This is what my Model looks like:

class Categorytree(models.Model):
    level = models.IntegerField(db_column='Level')  # Field name made lowercase.
    categorynode_idcategorynode = models.ForeignKey(Categorynode, models.DO_NOTHING, db_column='CategoryNode_idCategoryNode')  # Field name made lowercase.
    categorynode_idancestorcategorynode = models.ForeignKey(Categorynode, models.DO_NOTHING, db_column='CategoryNode_idAncestorCategoryNode', related_name='+')  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 'categorytree'
        unique_together = (('categorynode_idcategorynode', 'categorynode_idancestorcategorynode'),)

Note that it does not have a Primary Key and it shouldn't have, but Django seems to think there would be an Primary Key .id Field.

1
You have to have a primary key.Daniel Roseman
In the corresponding MySQL database design that table shouldn't have a primary key. Is it correct to add one in the models.py then?r00flr00fl

1 Answers

5
votes

To quote Django docs:

Each model requires exactly one field to have primary_key=True (either explicitly declared or automatically added).

If you need to access the table without primary key, you need to employ some other tool, like SQLAlchemy.