0

I have a small Django project with simple models. However, instead of creating my database via python manage.py syncdb I decided to create it manually and map the tables via Meta, as shown below

class Item(models.Model):
    name = models.CharField(max_length=50)

    class Meta:
            managed = False                                                   
            db_table = 'ITEM'

but this doesn't work. When I start the development server and run the main view, Django throws an error saying that the relation named ITEM doesn't exist in the database, when in fact it does exist.

I have done some research but couldn't find anyone with such a problem. Is there a way I can get it working?

Mauren
  • 1,955
  • 2
  • 18
  • 28
  • @DanielRoseman Yep. This is the weird thing. – Mauren Jan 28 '14 at 23:56
  • 1
    Could it be a capitalization issue? Postgres converts table names to lowercase unless the queries have quotes around them: http://binodsblog.blogspot.com/2011/02/postgresql-is-case-sensitive.html – Nils Jan 29 '14 at 00:20
  • @CantucciHQ WOW what a surprise, I'd never suspect that could be the problem. Please answer the question so I can accept it :D – Mauren Jan 29 '14 at 00:29
  • @Mauren That's SQL standard behaviour, btw, though the SQL spec says names should be _upper_-cased if not quoted. – Craig Ringer Jan 29 '14 at 02:47
  • @CraigRinger I'm more of an ORM person, since I spent nearly 4 years of my life working with JPA and Oracle database. I've never faced such a problem while using Oracle (see [this](http://stackoverflow.com/questions/7425153/reason-why-oracle-is-case-sensitive)), so this was a big surprise to me that Postgres does make case-sensitive identifier matching. – Mauren Jan 29 '14 at 12:16
  • 1
    @Mauren Oracle isn't known for following the spec closely ;-) . I'm not a fan of this particular SQL-spec behaviour. – Craig Ringer Jan 29 '14 at 12:38

2 Answers2

2

The db_table name should be in lowercase:

db_table = 'item'
Suchan Lee
  • 607
  • 5
  • 5
1

Postgres converts table names to lowercase letters unless the queries have quotes around them. See for example:

http://binodsblog.blogspot.com/2011/02/postgresql-is-case-sensitive.html

Normally that detail is abstracted away by the ORM, but if you don't use syncdb, you have to manage it.

Nils
  • 5,612
  • 4
  • 34
  • 37