Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Legacy database odd primary key fields
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
BenW  
View profile  
 More options Nov 6, 3:16 pm
From: BenW <benwil...@gmail.com>
Date: Fri, 6 Nov 2009 12:16:41 -0800 (PST)
Local: Fri, Nov 6 2009 3:16 pm
Subject: Legacy database odd primary key fields
Hello,

I am trying to write models for a legacy database that defines it's
primary keys as (MySQL):

bigint unsigned not null default '0'

On the surface, this seems like a poor decision because the
application then has to manage PK uniqueness itself.  My question is,
does anyone have any suggestions on how to define my models so that I
don't have to manage the uniqueness in MY application?  I'm happy to
wrap whatever field I need to and do a SELECT MAX(id) ... + 1 behind
the scenes to get a unique PK.

My ultimate goal is to encapsulate all the legacy tables in my models
so I can use the model as if it had an AutoField.

Any suggestions are welcome!

Thanks,

ben


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
akaariai  
View profile  
 More options Nov 6, 4:33 pm
From: akaariai <akaar...@gmail.com>
Date: Fri, 6 Nov 2009 13:33:38 -0800 (PST)
Local: Fri, Nov 6 2009 4:33 pm
Subject: Re: Legacy database odd primary key fields

On 6 marras, 22:16, BenW <benwil...@gmail.com> wrote:

> On the surface, this seems like a poor decision because the
> application then has to manage PK uniqueness itself.  My question is,
> does anyone have any suggestions on how to define my models so that I
> don't have to manage the uniqueness in MY application?  I'm happy to
> wrap whatever field I need to and do a SELECT MAX(id) ... + 1 behind
> the scenes to get a unique PK.

You can use a base model to inherit from for models that have such
primary keys. Write a save method something like this:

class Base:
    id = models.IntegerField(primary_key=True)
    class Meta:
          abstract = True

def save:
    if self.id set:
         call super save()
    else:
         self.id = get a cursor, execute proper sql to get unique id
         # Note that you probably want to inspect self._meta to be
able to
         # create the correct sql automatically.
         self.save(force_insert=True)

Using select max(id) + 1 is not safe, it can result in multiple
transactions getting the same id. You probably want to create a
sequence and get the id from the sequence.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
BenW  
View profile  
 More options Nov 6, 6:25 pm
From: BenW <benwil...@gmail.com>
Date: Fri, 6 Nov 2009 15:25:59 -0800 (PST)
Local: Fri, Nov 6 2009 6:25 pm
Subject: Re: Legacy database odd primary key fields
That definitely looks like a good approach.  By sequence, could I do
Model.objects.order_by('-id')[0].id + 1?

Also, what about using default= in the field constructor?  I was
thinking about setting default to a PK generator that gets a unique
key

Thanks,

Ben

On Nov 6, 1:33 pm, akaariai <akaar...@gmail.com> wrote:


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google