3

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 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Why they are writing a patch and not overriding the functions?
Sorry but I cannot find where to apply this patch... it seems to me that these files don't exist any more...
@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: 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: stephenjungels.com/jungels.net/articles/…
6 years later and still no solution to this problem in django.
1

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.

1 Comment

This doesn't answer the question, nor do I see how this is related.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.