views:

861

answers:

4

I whould like to use postgreSQL schemas with django, how can I do this?

+1  A: 

There is no explicit Django support for postgreSQL schemas.

When using Django (0.95), we had to add a search_path to the Django database connector for PostgreSQL, because Django didn't support specifying the schema that the tables managed by the ORM used.

Taken from:

http://nxsy.org/using-postgresql-schemas-with-sqlalchemy-and-elixir

The general response is to use SQLAlchemy to construct the SQL properly.

Oh, and here's another link with some suggestions about what you can do with the Django base, extending it to try to support your scheme:

http://news.ycombinator.com/item?id=662901

AlbertoPL
thanks AlbertoPL, I will take a look at these links
Nako
A: 

I've had some success just saying

db_table = 'schema\".\"tablename'

in the Meta class, but that's really ugly. And I've only used it in limited scenarios - it may well break if you try something complicated. And as said earlier, it's not really supported...

Magnus Hagander
+2  A: 

I've been using:

db_table = '"schema"."tablename"'

in the past without realising that only work for read-only operation. When you try to add new record it would fail because the sequence would be something like "schema.tablename"_column_id_seq.

db_table = 'schema\".\"tablename'

does work so far. Thanks.

k4ml
I don't use PostgreSQL, but this is how I make it work in SQL Server too.
celopes
Any trick for creating the schema automatically in syncdb before creating tables? Necessary for running test suites.
akaihola
+3  A: 

It's a bit more complicated then fancy then tricky escaping. Have a look at Ticket #6148 in Django for perhaps a solution or at least a patch. It does some minor changes deep in the django.db core but it will hopefully be officially included in django. After that it's just a matter of saying

db_schema = 'whatever_schema'

in the Meta class or for a global change set

DATABASE_SCHEMA = 'default_schema_name'

in settings.py

birchroad