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 564605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:47:03+00:00 2026-05-13T12:47:03+00:00

Given the following table in PostgreSQL, how do I insert a record which refers

  • 0

Given the following table in PostgreSQL, how do I insert a record which refers to itself?

CREATE TABLE refers (
    id        SERIAL  PRIMARY KEY,
    name      VARCHAR(255) NOT NULL,
    parent_id INTEGER      NOT NULL,
    FOREIGN KEY (parent_id) REFERENCES refers(id)
);

The examples I’m finding on the Web have been allowed the parent_id to be NULL and then use a trigger to update it. I’d rather update in one shot, if possible.

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

    You can select last_value from the sequence, that is automatically created when you use type serial:

    create table test (
      id serial primary key,
      parent integer not null,
      foreign key (parent) references test(id)
    );
    
    insert into test values(default, (select last_value from test_id_seq));
    insert into test values(default, (select last_value from test_id_seq));
    insert into test values(default, (select last_value from test_id_seq));
    
    select * from test;
     id | parent
    ----+--------
      1 |      1
      2 |      2
      3 |      3
    (3 rows)
    

    And the following even simpler seems to work as well:

    insert into test values(default, lastval());
    

    Though I don’t know how this would work when using multiple sequences… I looked it up; lastval() returns the last value returned or set with the last nextval or setval call to any sequence, so the following would get you in trouble:

    create table test (
      id serial primary key,
      foo serial not null,
      parent integer not null,
      foreign key (parent) references test(id)
    );
    
    select setval('test_foo_seq', 100);
    
    insert into test values(default, default, lastval());
    ERROR:  insert or update on table "test" violates foreign key constraint "test_parent_fkey"
    DETAIL:  Key (parent)=(101) is not present in table "test".
    

    However the following would be okay:

    insert into test values(default, default, currval('test_id_seq'));
    
    select * from test;
     id | foo | parent
    ----+-----+--------
      2 | 102 |      2
    (1 row)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 498k
  • Answers 498k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Do you have Resharper installed? If yes then this how… May 16, 2026 at 12:24 pm
  • Editorial Team
    Editorial Team added an answer It will just upgrade your existing version unless you change… May 16, 2026 at 12:24 pm
  • Editorial Team
    Editorial Team added an answer If the structures are plain POD (no pointer internals) then… May 16, 2026 at 12:24 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Given a Postgresql table schema: create table thing ( id serial primary key, key
Given the following tables: CREATE TABLE tree ( id serial NOT NULL, name character
say I have a postgresql table with the following values: id | value ----------
Given the following indexes for an Oracle database: CREATE INDEX subject_x1 ON subject (code);
I have the following database table with information about people, diseases, and drugs: PERSON_T
Given SQL as an input, I have to query a PostgreSQL database and return
given the following tables, how would I go about finding the most common ip
I'm thinking of the best way to design a database given the following situation.
Following is the table and script of this table. DECLARE @temp TABLE (PPId INT,
Assume I have a table that has the following structure: ================= | Id |

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.