1

My database has the tables books[isbn,name,author,availableNum],students[name,major,coursestaking],course[name,department,number,capacity,booksNeeded],department[name,courses] I have a small view that has fields from all the above {courseName,booksRecommended,dept,countofstudents}. When creating a model for view should I say something like this.

class Lookup(models.Model):
    course = models.ForeignKey('course',db_column = 'name')
    booksRecommended = models.ForeignKey('books',db_column = 'isbn')
    dept = models.ForeignKey('department',db_column = 'name')
    studentcount = models.IntegerField()

I'm getting the error ProgrammingError: column lookup.id does not exist. Ideas are welcome

1 Answer 1

2

The models.Model that you have inherited from assumes that the table (or the view, in your case) has a column called as id that is used as a primary key, unless you have explicitly marked some other column to be primary_key = True.

Your view's sql, or the ./manage.py inspectdb should help us find the primary key for this view.

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

4 Comments

this view doesn't have a primary key.so when i did ./manage.py inspectdb i couldnt see. but making one of the column(which was unique) to primary_key=True, did solve the issue.Thanks Lakshman
but in the views should i specify the relation explicitly, or as the database view already is aware of the constraints do i have to worry about it?
If the name of the primary key is id, django can guess it, otherwise, you need to specify it explicitly.
The fact I missed this autoId pattern caused me to lose some time. Adding primary_key = True to any field on the model for the view that will always be not null not blank solved it!

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.