Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5936187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:21:48+00:00 2026-05-22T15:21:48+00:00

I had a custom primary key that need to be set up on a

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:21:49+00:00Added an answer on May 22, 2026 at 3:21 pm

    Use an AutoField with primary_key instead.

    Edit:

    If you don’t use an AutoField, you’ll have to manually calculate/set the value for the primary key field. This is rather cumbersome. Is there a reason you need ReportNumber to the primary key? You could still have a unique report number on which you can query for reports, as well as an auto-incrementing integer primary key.

    Edit 2:

    When you say duplicate primary key values are allowed, you indicate that what’s happening is that an existing record with the same primary key is updated — there aren’t actually two objects with the same primary key in the database (which can’t happen). The issue is in the way Django’s ORM layer chooses to do an UPDATE (modify an existing DB record) vs. an INSERT INTO (create a new DB record). Check out this line from django.db.models.base.Model.save_base():

    if (force_update or (not force_insert and
            manager.using(using).filter(pk=pk_val).exists())):
        # It does already exist, so do an UPDATE.
    

    Particularly, this snippet of code:

    manager.using(using).filter(pk=pk_val).exists()
    

    This says: “If a record with the same primary key as this Model exists in the database, then do an update.” So if you re-use a primary key, Django assumes you are doing an update, and thus doesn’t raise an exception or error.


    I think the best idea is to let Django generate a primary key for you, and then have a separate field (CharField or whatever) that has the unique constraint.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We had our own custom web server(written in java) built in 2005. We are
My Application requires the calendar control and i had downloaded the custom calendar control
Had a nice little custom button with a few parameters I was POSTing to
I had to implement a custom HttpModule to handle a 404 error in Sharepoint.
Has anyone had success creating a custom list component which accepts a user defined
[python 2.6 - django 1.1.1] Hello. I'm writing a custom serializer for my django
On Android, I would like to create a button that contains some other Views.
I've been using Rails 2.3.8 for a project that I've been working on, but
I'm facing a conundrum here. One of the applications that I've developed is loading
What I want to do is use DataAnnotation to identify validation on my model

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.