Friday, October 31, 2014

Primary keys vs. natural keys

Hallöchen!

Do I understand it correctly that in the Django community, it is
preferred to use surrogate primary keys (the auto ID field) instead
of explicitly setting primary keys? Currently, I add natural_key()
methods to many of my models, but some of them return only one
field. Now I wonder whether I made a mistake and should have chosen
that field as the primary key.

My use case is that I need to retrieve the natural keys of all
objects in the database of a particular model. As far as I can see,
this can only be achieved by fetching all objects and calling the
natural_key() method for all of them. In contrast,

model.objects.values_list("pk", flat=True)

is probably *much* faster.

Is it possibly to specify a single field a natural key somehow
(besides making it the PK)? For example, it is possible to add a
Meta attribute to the model?

Tschö,
Torsten.

--
Torsten Bronger Jabber ID: torsten.bronger@jabber.rwth-aachen.de
or http://bronger-jmp.appspot.com

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/87egtoha9w.fsf%40physik.rwth-aachen.de.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment