Django

Code

Ticket #8901 (new)

Opened 3 months ago

Last modified 3 months ago

last_insert_id() for postgres fails when the autoincrement sequence name is too long.

Reported by: adam@zuerchertech.com Assigned to: nobody
Milestone: Component: Database layer (models, ORM)
Version: 1.0 Keywords:
Cc: Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

The autogenerated sequence name for a primary key in postgresql is "tablename_pkname_seq". last_insert_id() constructs this name from the tablename and the pkname, but it doesn't correctly truncate it if it's too long. The sequence name must be <= 63 characters. If it's too long, then the tablename and pkname are truncated to 29 characters each. It's possible for the tablename to be 28 characters and the pkname to be 30 characters. 30 is greater than 29, but the whole thing is exactly 63 characters, so no truncation is needed. I've included a patch against Django 1.0. It's been tested on postgres 8.1 on Windows. I have not tried any other versions.

Attachments

postgres_sequence_name.patch (1.0 kB) - added by adam@zuerchertech.com on 09/05/08 10:33:51.

Change History

09/05/08 10:33:51 changed by adam@zuerchertech.com

  • attachment postgres_sequence_name.patch added.

09/05/08 11:14:56 changed by mtredinnick

  • needs_better_patch changed.
  • stage changed from Unreviewed to Accepted.
  • needs_tests changed.
  • needs_docs changed.

Nice catch. This patch does need a comment, but the one you've got is confusing, since it leaves out the reason for what's going on. A comment that just explains why we're doing the truncating is better. It also looks very, very fragile: is the way this sequence name is constructed documented anywhere? I'd like some confirmation that we aren't going to be chasing versions of PostgreSQL forever with changing things here (particularly since 8.1 is now a couple of versions behind the latest). It might be more robust to also change the SQL creation code to explicitly specify the sequence name, as we do with a lot of index names to ensure they fit inside the 63 character limit.

Anyway, we should definitely fix the problem here, including doing so for existing setups. Precisely how needs a bit of research to make sure it's robust.


Add/Change #8901 (last_insert_id() for postgres fails when the autoincrement sequence name is too long.)




Change Properties
Action