Opened 13 years ago

Closed 10 years ago

Last modified 9 years ago

#1208 closed defect (duplicate)

db_table with schema name does not work on PostgreSQL

Reported by: slamb@… Owned by: Adrian Holovaty
Component: Database layer (models, ORM) Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


If I give Django a db_table of "loadtest.machine", it passes to PostgreSQL "loadtest.machine", in quotes. PostgreSQL wants "loadtest"."machine". There needs to be some way of passing in a schema-qualified table name, such as:

  • pass in the latter, at the expense of not supporting an actual '.' in the name).
  • support a db_schema attribute, maybe, so there's no ambiguity.

My code used to work, but Django added some quoting.

Here's an example of the errors pgsql gives:

loadtest=# select count(*) from "loadtest"."experiment";
(1 row)

loadtest=# select count(*) from "loadtest.experiment";
ERROR:  relation "loadtest.experiment" does not exist

Change History (6)

comment:1 Changed 13 years ago by Adrian Holovaty

See also [1051].

comment:2 Changed 13 years ago by Jacob

Resolution: duplicate
Status: newclosed

duplicate of #1051

comment:3 Changed 10 years ago by jxh

Resolution: duplicate
Status: closedreopened

1051 et seq don't quite address this concern. They only want to let you set the search_path, once per application or per database connection. If your application needs to talk to an existing postgresql database with data in multiple schemas, you need to be able to express table names as 'a.b' and have them appear as 'a'.'b' in SQL. In fact, search_path need never be used.

comment:4 Changed 10 years ago by Ramiro Morales

I'd say this ticket can be re-closed. #6148 is the ticket tracking implementation of a generic support for specifying DB schemas for model tables feature.

comment:5 Changed 10 years ago by Russell Keith-Magee

Resolution: duplicate
Status: reopenedclosed

Agreed. Dupe of #6148.

comment:6 Changed 9 years ago by Antti Kaihola

For anyone looking for a quick work-around, MariusBoo noted that you can define your table like this:

db_table = '"django"."company"'

This will fool the quote function to think that your table is properly quoted. This also means that you have to specify the schema for each table manually.

I just verified that this solution works with r10837 and r11802 and PostgreSQL 8.3.8.

Note: See TracTickets for help on using tickets.
Back to Top