On Jul 18, 7:41 am, Torsten Bronger <[EMAIL PROTECTED]>
wrote:
> > Can I ask why you would need to change PKs?
>
> Yes.  We are a scientific institution, and I create a samples
> database.  Frequently a sample gets a new name, however, the old
> name(s) must be kept as aliases.
>
> For the aliases, I probably will use a second table with many-to-one
> relationship.
>
> I wanted to keep the (current) sample name the primary key because I
> think that this accelerates lookup, but if all else fails, I must
> use a surrogate key.

Clearly, a growing set of names is not a sufficiently unique and
immutable primary key for your data. I'd suggest something like a
composite key made of date/time, sample no.,  and fk to experiment
(whatever an experiment's pk is), but django doesn't support composite
keys. So a "surrogate" is a good compromise here. As for names, since
each sample can have multiple names, it should be a separate table
with a foreign key to the samples table.

class Experiment(models.Model):
    date = ...
    etc ....

class Sample(models.Model):
    experiment = models.ForeignKey(Experiment)
    date = models.DateTimeField()
    sampleno = models.PositiveIntegerField()
    ... data ...
   class Meta:
       unique_together = (('experiment', 'date', 'sampleno'),)

class SampleName(models.Model):
    sample = models.ForeignKey(Sample)
    name = models.CharField(max_length = 60)
    named = models.DateTimeField()

Primary keys never change over time.

-Jim
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to