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

  • SEARCH
  • Home
  • 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 6555287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:48:04+00:00 2026-05-25T12:48:04+00:00

We’ve got a small problem with a Django project we’re working on and our

  • 0

We’ve got a small problem with a Django project we’re working on and our postgresql database.

The project we’re working on is a site/db conversion from a PHP site to a django site. So we used inspect db to generate the models from the current PHP backend.

It gave us this and we added the primary_key and unique equals True:

class Company(models.Model):
     companyid = models.IntegerField(primary_key=True,unique=True)
     ...
     ...

That didn’t seem to be working when we finally got to saving a new Company entry. It would return a not-null constraint error, so we migrated to an AutoField like below:

class Company(models.Model):
     companyid = models.AutoField(primary_key=True)
     ...
     ...

This saves the Company entry fine but the problem is when we do

result = form.save()

We can’t do

result.pk or result.companyid

to get the newly given Primary Key in the database (yet we can see that it has been given a proper companyid in the database.

We are at a loss for what is happening. Any ideas or answers would be greatly appreciated, thanks!

  • 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-25T12:48:04+00:00Added an answer on May 25, 2026 at 12:48 pm

    I just ran into the same thing, but during a django upgrade of a project with a lot of history. What a pain…

    Anyway, the problem seems to result from the way django’s postgresql backend gets the primary key for a newly created object: it uses pg_get_serial_sequence to resolve the sequence for a table’s primary key. In my case, the id column wasn’t created with a serial type, but rather with an integer, which means that my sequence isn’t properly connected to the table.column.

    The following is based on a table with the create statement, you’ll have to adjust your table names, columns and sequence names according to your situation:

    CREATE TABLE "mike_test" (
      "id" integer NOT NULL PRIMARY KEY,
      "somefield" varchar(30) NOT NULL UNIQUE
    );
    

    The solution if you’re using postgresql 8.3 or later is pretty easy:

    ALTER SEQUENCE mike_test_id_seq OWNED BY mike_test.id;
    

    If you’re using 8.1 though, things are a little muckier. I recreated my column with the following (simplest) case:

    ALTER TABLE mike_test ADD COLUMN temp_id serial NOT NULL;
    UPDATE mike_test SET temp_id = id;
    ALTER TABLE mike_test DROP COLUMN id;
    ALTER TABLE mike_test ADD COLUMN id serial NOT NULL PRIMARY KEY;
    UPDATE mike_test SET id = temp_id;
    ALTER TABLE mike_test DROP COLUMN temp_id;
    SELECT setval('mike_test_id_seq', (SELECT MAX(id) FROM mike_test));
    

    If your column is involved in any other constraints, you’ll have even more fun with it.

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

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
I am currently running into a problem where an element is coming back from
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into

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.