Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a postgreSQL database that has a table foo that I've created outside of django. I used manage.py inspectdb to build the model for table foo for me. This technique worked fine when I was using MySQL but with PostgreSQL it is failing MISERABLY. The table is multiple gigabytes and I build it from a text file with PostgreSQL 'COPY'.

I can run raw queries on table foo and everything executes and expected.

For example

foo.objects.raw('bar_sql')

executes as expected.

But running queries like:

foo.objects.get(bar=bar)

throw

ProgrammingError column foo.id does not exist LINE 1: SELECT "foo"."id", "foo"."bar1", "all_...

foo doesn't innately have an id field. As I understand it django is suppose to create one. Have I some how subverted this step when creating the tables outside of django?

Queries run on models whose table was populated threw django run as expected in all cases.

I'm missing something very basic here and any help would be appreciated.

I'm using django 1.6 with postgreSQL 9.3.

share|improve this question
    
How does your table and model look like? Django may have failed to recognize wich column is the PK, hence it looks for "id", the default. Your model should have a field with pimary_key =True –  Alvaro Jan 17 at 19:26
    
Thank you for your help. Youre absolutely correct. I never set a primary key. –  user3204587 Jan 17 at 19:49

1 Answer 1

up vote 0 down vote accepted

Django doesn't modify your existing database tables. It only creates new tables. If you have existing tables, it usually doesn't touch them at all.

"As I understand it django is suppose to create one." --> It only adds a primary key to a table when it creates it, which means you don't need to specify that explicitly in your model, but it won't do anything to an existing table.

So if for example you later on decide to add fields to your models, you have to update your databases manually.

What you need to do in your case is that by doing manual database administration make sure that your table has a primary key, and also that the name of the primary key is "id" (although I am not sure if this is necessary, it is better to do it.) So use a database administration tool, modify your table and add the primary key, and name it id. Then it should start working.

share|improve this answer
    
Thank you very much for your help. –  user3204587 Jan 17 at 20:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.