views:

260

answers:

2

I have this model in django:

class JournalsGeneral(models.Model):
    jid = models.AutoField(primary_key=True)
    code = models.CharField("Code", max_length=50)
    name = models.CharField("Name", max_length=2000)
    url = models.URLField("Journal Web Site", max_length=2000, blank=True)
    online = models.BooleanField("Online?")
    active = models.BooleanField("Active?")
    class Meta:
        db_table = u'journals_general'
        verbose_name = "Journal General"
        ordering = ['code']
    def __unicode__(self):
        return self.name

My problem is that in the DB (Postgres) the name of the sequence connected to jid is not journals_general_jid_seq as expected by Django but it has a different name.

Is there a way to specify which sequence Django has to use for an AutoField? In the documentation I read I was not able to find an answer.

+2  A: 

I don't think so, but there is a ticket open for that (with a possibly useful patch?):

http://code.djangoproject.com/ticket/1946

Edit:

Actually, that link above has a comment from HM on the thread with a better solution than the patch itself. The patch is old, I'm unsure if it will be applicable to the version of Django you are using.

celopes
Why they are writing a patch and not overriding the functions?
Giovanni Di Milia
Sorry but I cannot find where to apply this patch...it seems to me that these files don't exist any more...
Giovanni Di Milia
@Giovanni: They aren't writing the patch - someone noticed the problem just like you and contributed a patch. The patch - or an alternative - might end up in the trunk of the project at some point (or not, some patches are never deemed worthy). The patch is there, use this link: http://code.djangoproject.com/attachment/ticket/1946/source-patch and use the download link at the bottom. You will need to use `patch` to apply it. If you need help to use `patch`, this blog post might be helpful: http://stephenjungels.com/jungels.net/articles/diff-patch-ten-minutes.html
celopes
@Giovanni: Actually, see my edit to the answer above.
celopes
+2  A: 

What about this patch in Django: http://code.djangoproject.com/ticket/8901 ? And do you realy need the name of the sequence? As of version 8.2, PostgreSQL has RETURNING that you can use in INSERT's (and UPDATE and DELETE) that can be used to get the created id:

INSERT INTO foo(bar) VALUES('John') RETURNING id;

I have no idea if Django can handle this (fetching an INSERT-result), but it's a nice thing and also good for performance.

Frank Heikens