Questions tagged [natural-key]

A key in a database table that is used to identify entities in the business domain outside the database. It may be comprised of attributes that have intrinsic logical meaning.

The term natural key describes a key which is used for external real world identification of things and is different from a surrogate key which is not used in the business domain.

57 questions
39
votes
7 answers

What is a natural identifier in Hibernate?

While reading through the Hibernate documentation, I keep seeing references to the concept of a natural identifier. Does this just mean the id an entity has due to the nature of the data it holds? E.g. A user's name + password + age + something…
benstpierre
  • 32,833
  • 51
  • 177
  • 288
15
votes
10 answers

Relational database design question - Surrogate-key or Natural-key?

Which one is the best practice and Why? a) Type Table, Surrogate/Artificial Key Foreign key is from user.type to type.id: b) Type Table, Natural Key Foreign key is from user.type to type.typeName:
aryaxt
  • 76,198
  • 92
  • 293
  • 442
12
votes
3 answers

Is it possible to use a natural key for a GenericForeignKey in Django?

I have the following: target_content_type = models.ForeignKey(ContentType, related_name='target_content_type') target_object_id = models.PositiveIntegerField() target = generic.GenericForeignKey('target_content_type', 'target_object_id') I would…
Riz
  • 123
  • 2
  • 5
7
votes
4 answers

Automate the generation of natural keys

I'm studying a way to serialize part of the data in database A and deserialize it in database B (a sort of save/restore between different installations) and I've had a look to Django natural keys to avoid problems due to duplicated IDs. The only…
Don
  • 16,928
  • 12
  • 63
  • 101
7
votes
3 answers

What's the advantage of finding an entity using the Hibernate @NaturalId

Is there any advantage to find an object using the Hibernate's @NaturalId? I'm concerned about the fact that Hibernate performs two queries to get an object using @NaturalId. The first query just to get the id and the second query to load the real…
bruno.zambiazi
  • 1,422
  • 14
  • 19
7
votes
9 answers

Unique identifiers for users

If I have a table of a hundred users normally I would just set up an auto-increment userID column as the primary key. But if suddenly we have a million users or 5 million users then that becomes really difficult because I would want to start…
christophmccann
  • 4,181
  • 7
  • 42
  • 66
7
votes
1 answer

Difference between get_by_natural_key and natural_key

As I understand, the model manager's get_by_natural_key is used in deserialization and the natural_key is used in serialization. Is this true ? If not what are the differences ? And also, do we need to give the --natural-foreign and…
Mohan
  • 1,850
  • 1
  • 19
  • 42
7
votes
1 answer

Can a model manager access its models' Meta attribute (`Meta.unique_together`)?

Here's my attempt at a generalized natural key model manager. It's like the docs except it tries (unsuccessfully) to determine the natural key field names from the Meta.unique_together attribute. class NaturalKeyModelManager(Manager): def…
hobs
  • 18,473
  • 10
  • 83
  • 106
5
votes
1 answer

How to model a one-to-one relationship in JPA when the "parent" table has a composite PK?

While there is plenty of information around on how to model, in JPA (2), a one-to-one relationship OR an entity having a natural key, I haven't been able to find a clear / simple answer to how to model the situation where we have both, i.e. a…
Octavian Theodor
  • 537
  • 6
  • 14
5
votes
2 answers

Django : Generic Foreign key dumpdata: Can't resolve dependencies

I use Generic Foreign keys to relate different profiles with my Users model which is inherited from auth.User. I'm not able to do dumpdata though with the --natural option passed. It says, Error: Can't resolve dependencies for…
Babu
  • 2,548
  • 3
  • 30
  • 47
5
votes
1 answer

Django Natural Key for Fixtures Give Deserialization Error

I've seen a few similar questions to this on SO, but none seem to answer my particular problem. I'm new to Django and was guiding myself by the instructions at this page to allow myself to use natural keys to load fixtures. Nevertheless, I'm getting…
Eli
  • 36,793
  • 40
  • 144
  • 207
3
votes
1 answer

How can I update a MySQL natural key while avoiding duplicate foreign keys?

Here is the thing: I have a webapp built with jQuery/php/mysql. It's kind of a task manager. I have categories and tasks belonging to categories. Tasks have a surrogate primary key (Autoincrement id) and for database consistency I have a natural key…
Kev
  • 5,049
  • 5
  • 32
  • 53
3
votes
2 answers

Loading a Django Fixture containing Natural Keys

How do you load a Django fixture so that models referenced via natural keys don't conflict with pre-existing records? I'm trying to load such a fixture, but I'm getting IntegrityErrors from my MySQL backend, complaining about Django trying to insert…
Cerin
  • 60,957
  • 96
  • 316
  • 522
3
votes
5 answers

Entity Framework and associations between string keys

I am new to Entity Framework, and ORM's for that mather. In the project that I'm involed in we have a legacy database, with all its keys as strings, case-insensitive. We are converting to MSSQL and want to use EF as ORM, but have run in to a…
fredrik
  • 13,282
  • 4
  • 35
  • 52
3
votes
2 answers

Natural Key and Fact tables

I'm new on dimensional modelling I believe that you guys can help me in the following doubts. In the production system I have a transaction table, sales table for example.The unique identifier is a primary key called SaleId. Example: My doubt is…
1
2 3 4