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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:16:36+00:00 2026-06-17T12:16:36+00:00

I have such table: — — PostgreSQL database dump — SET statement_timeout = 0;

  • 0

I have such table:

--
-- PostgreSQL database dump
--

SET statement_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;

SET search_path = public, pg_catalog;

SET default_tablespace = '';

SET default_with_oids = false;

--
-- Name: forum; Type: TABLE; Schema: public; Owner: postgres; Tablespace: 
--

CREATE TABLE forum (
    forum_id integer DEFAULT nextval('seq_forum'::regclass) NOT NULL,
    forum_name character varying NOT NULL,
    group_id integer NOT NULL,
    forum_parent integer DEFAULT (-1)
);


ALTER TABLE public.forum OWNER TO postgres;

--
-- Name: PK_forum; Type: CONSTRAINT; Schema: public; Owner: postgres; Tablespace: 
--

ALTER TABLE ONLY forum
    ADD CONSTRAINT "PK_forum" PRIMARY KEY (forum_id);


--
-- Name: FK_group; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--

ALTER TABLE ONLY forum
    ADD CONSTRAINT "FK_group" FOREIGN KEY (group_id) REFERENCES groups(group_id) ON UPDATE CASCADE ON DELETE CASCADE;


--
-- Name: FK_parent; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--

ALTER TABLE ONLY forum
    ADD CONSTRAINT "FK_parent" FOREIGN KEY (forum_parent) REFERENCES forum(forum_id);


--
-- PostgreSQL database dump complete
--

As you see above, this table has (at least should have …) a default value in column forum_parent. I want to insert some data to this table, I do it like this:

INSERT INTO forum (forum_name, group_id) VALUES('new forum', 1); 

yup, I have a group with id = 1. But this code gives me:

PostgreSQL error: 23503 ERROR:  insert or update on table "forum" violates foreign key constraint "FK_parent"
DETAIL:  Key (forum_parent)=(-1) is not present in table "forum".

NOTICE:  there is no transaction in progress

How to make it right?

  • 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-06-17T12:16:37+00:00Added an answer on June 17, 2026 at 12:16 pm

    Your INSERT statement is correct. When you don’t explicity declare the column name on your INSERT statement, the value that is inserted on the table is the default (in your case, it’s -1).

    But the problem lies on the referential constraint. The column forum_parent of table forum is dependent on the values of column forum_id of the same table. As this DDL says,

    ALTER TABLE ONLY forum
           ADD CONSTRAINT "FK_parent" FOREIGN KEY (forum_parent) 
           REFERENCES forum(forum_id);
    

    The INSERT statement failed during execution because the value -1 is not present on column forum_id.

    My suggestion is to change the default value from -1 to NULL

    CREATE TABLE forum 
    (
        forum_id integer DEFAULT nextval('seq_forum'::regclass) NOT NULL,
        forum_name character varying NOT NULL,
        group_id integer NOT NULL,
        forum_parent integer DEFAULT NULL
    );
    

    The difference between NULL and -1 is that NULL is simply unknow. or the value does not exist while -1 is an existing numeric value.

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

Sidebar

Related Questions

I have to write a query to database, that has such table: TABLE HISTORY:
I have such table in my MySQL database: --------------------------- |fid | price | date
In an SQL Server 2008 database, I have tables such as: CREATE TABLE t_DeviceType
I have such question for database developers. I have a table which has two
i have such mysql db table: id name surname number 1 emil asadi 26
Suppose I have a system where I have metadata such as: table: ====== key
I have a table such as: PK_ID LOCATION_ID LOCATION_TEXT 1 21 NULL 2 44
I have a table such as: table a +------+-------+ | v1 | v2 |
If I have a table such as: info_name | info_value name | Susan desc
I have such code in my JSF template: <h:form> <table id=users cellspacing=0> <a4j:repeat var=person

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.