开发者

django loaddata caused ProgrammingError: "..." is not a sequence when loading a long table name in postgresql

I'm loading a fixture generated from sqlite into postgresql database using manage.py loaddata. The loading failed when it reaches a ManyToManyField with a long name.

I traced into the database adapter: usr/local/lib/python2.6/dist-packages/django/db/backends/postgresql_psycopg2/base.py line 44

return self.cursor.exe开发者_开发问答cute(query, args)

the value of query is correct: 'SELECT CURRVAL(\'"a_long_long_table_name_id_seq"\')'

but then it failed with DatabaseError: "a_long_long_table_name_i" is not a sequence

Obviously the name is cut off after executing this statement, but I can't trace deeper.

django version: 1.2

How to fix this problem?


I found the answer from http://archives.postgresql.org/pgsql-novice/2003-06/msg00239.php, postgres table name has a maximum limit of 63. To increase that limit, one has to change its source code and set NAMEDATALEN to higher value. But the problem is, in django, it's really easy to exceed this limit because the association table names for many-to-many fields usually are in this format: application_name_model1_name_model2_name

Anyway, I decided to shorten the model names and not re-compile the source code.


You know that you can use app labels and db_table to specifically tell the database what name should be used as a table name, right? This means that you shouldn't need to shorten the names of Models, but keep them descriptive.

As far as I know, Postgres will truncate the name of a database table (and fields) on creation, using a function that will always yield the same name. This should mean that only the fixtures need to be modified. Determine the actual name within Postgres, and do a find replace on the fixture data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜