views:

1177

answers:

3
class dbview(models.Model):
    # field definitions omitted for brevity
    class Meta:
        db_table = 'read_only_view'

def main(request):
    result = dbview.objects.all()

Caught an exception while rendering: (1054, "Unknown column 'read_only_view.id' in 'field list'")

There is no primary key I can see in the view. Is there a workaround?

Comment:
I have no control over the view I am accessing with Django. MySQL browser shows columns there but no primary key.

A: 

There should have been an auto-generated id field when you ran syncdb (if there is no primary key defined in your model, then Django will insert an AutoField for you).

This error means that Django is asking your database for the id field, but none exists. Can you run django manage.py dbshell and then DESCRIBE read_only_view; and post the result? This will show all of the columns that are in the database.

Alternatively, can you include the model definition you excluded? (and confirm that you haven't altered the model definition since you ran syncdb?)

obeattie
Thanks, I should have made it more clear in the beginning. Please see the update.
dmi
I'm not asking to see the row you are accessing, I just want to see your model definition (not the same thing).
obeattie
The model definitions are like this: text = models.CharField() However, I cannot define a field with 'primary_key=True' because the view does not seem to have a primary key.
dmi
Sorry to be so blunt, but do you know what a primary key is?
obeattie
The way I see the problem: the view does not have a primary key (I can't see it), Django requires each model to have a primary key (either manually defined by 'primary_key=True' or default 'id' if not defined by user).
dmi
No matter if I define a primary key in my model or skip this, Django will still require a primary key to be in a view I am trying to query...
dmi
+1  A: 

If there really is no primary key in the view, then there is no workaround.

Django requires each model to have exactly one field primary_key=True.

webjunkie
Thank you! If I cannot see primary key in the view it means that there is no primary key...? Guess I'll just revert to raw SQL.
dmi
Not necessarily. See my comment below.
andybak
+4  A: 

When you say 'I have no control over the view I am accessing with Django. MySQL browser shows columns there but no primary key.'

I assume you mean that this is a legacy table and you are not allowed to add or change columns?

If so and there really isn't a primary key (even a string or non-int column*) then the table hasn't been set up very well and performance might well stink.

It doesn't matter to you though. All you need is a column that is guaranteed to be unique for every row. Set that to be 'primary_key = True in your model and Django will be happy.

  • There is one other possibility that would be problemmatic. If there is no column that is guaranteed to be unique then the table might be using composite primary keys. That is - it is specifying that two columns taken together will provide a unique primary key. This is perfectly valid relational modelling but unfortunatly unsupported by Django. In that case you can't do much besides raw SQL unless you can get another column added.
andybak
Thank you so much, this is really really helpful!
dmi
Very helpful. One thing to add; if the column you want to use for a primary key is a CHAR type, it can't be longer than 255 characters.
John Weldon