Primary key and unique key in django
NickName:Nagaraj Tantri Ask DateTime:2011-05-18T11:05:59

Primary key and unique key in django

I had a custom primary key that need to be set up on a particular data in a model.

This was not enough, as an attempt to insert a duplicate number succeeded. So now when i replace primary_key=True to unique=True it works properly and rejects duplicate numbers!!. But according this document (which uses fields).

primary_key=True implies null=False and unique=True.

Which makes me confused as in why does it accept the value in the first place with having an inbuilt unique=True ?

Thank you.

Updated statement:

   personName = models.CharField(primary_key=True,max_length=20)

Copyright Notice:Content Author:「Nagaraj Tantri」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/6039443/primary-key-and-unique-key-in-django

More about “Primary key and unique key in django” related questions

Primary key and unique key in django

I had a custom primary key that need to be set up on a particular data in a model. This was not enough, as an attempt to insert a duplicate number succeeded. So now when i replace primary_key=Tru...

Show Detail

Compound/Composite primary/unique key with Django

How can you create models (and thus tables) with a compound (composite) primary/unique key using Django?

Show Detail

django Charfield suitable for a primary key?

In my django model I would like to set the field unique_id below to be the primary key as this will be the field used in queries for the model. It satisfies unique=True and null=False. However as ...

Show Detail

Django Fake Primary Key

I have a legacy database which I have no control over and I am trying to create a model for it in Django. It has a composite primary key and I do not wish to create dirty hacks to achieve it in Dja...

Show Detail

Doubts about Django models primary_key and UniqueConstraint for composite primary key

Usually primary key means also unique (and more conditions like not null, etc.). If I have UniqueConstraint with 2 fields, including the primary key, I have 2 unique conditions (one because the pri...

Show Detail

Django datetime primary key doesn't constrain unique

So, here's my model: class MyModel(models.Model): timestamp = models.DateTimeField(primary_key=True) fielda = models.TextField() When I call syncdb, django doesn't add a constraint to post...

Show Detail

Oracle : Unique Key and Primary Key

Consider the table which contains unique Key and Primary Key .the tables already contains data.If i added any rows to the table i received an error (ORA - 0001) which is due to the duplicate value is

Show Detail

Change Primary Key field to unique field

When setting up my database structure, I was stupid enough to set a field called "student_id" as "primary_key=True" for my Student class. I only realised much later that there are (rare) occasions ...

Show Detail

django not raising IntegrityError for duplicate primary key

Does django enforce uniqueness for a primary key? The documentation here seems to suggest so, but when I define a class as: class Site(models.Model): id = models.IntegerField(primary_key=True...

Show Detail

Django Model Auto Increment Primary Key Based on Foreign Key

I'm trying to figure out how to lay out two of my Django models so that when a new model is saved to the database, its primary key is incremented such that it is the next highest value for all reco...

Show Detail