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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:40:03+00:00 2026-06-16T18:40:03+00:00

I would like to delete rows which contain a foreign key, but when I

  • 0

I would like to delete rows which contain a foreign key, but when I try something like this:

DELETE FROM osoby WHERE id_osoby='1'

I get this statement:

ERROR: update or delete on table “osoby” violates foreign key constraint “kontakty_ibfk_1” on table “kontakty”
DETAIL: Key (id_osoby)=(1) is still referenced from table “kontakty”.

How can I delete these rows?

  • 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-16T18:40:04+00:00Added an answer on June 16, 2026 at 6:40 pm

    To automate this, you could define the foreign key constraint with ON DELETE CASCADE.
    I quote the the manual for foreign key constraints:

    CASCADE specifies that when a referenced row is deleted, row(s)
    referencing it should be automatically deleted as well.

    Look up the current FK definition like this:

    SELECT pg_get_constraintdef(oid) AS constraint_def
    FROM   pg_constraint
    WHERE  conrelid = 'public.kontakty'::regclass  -- assuming public schema
    AND    conname = 'kontakty_ibfk_1';
    

    Then add or modify the ON DELETE ... part to ON DELETE CASCADE (preserving everything else as is) in a statement like:

    ALTER TABLE kontakty
       DROP CONSTRAINT kontakty_ibfk_1
     , ADD  CONSTRAINT kontakty_ibfk_1
       FOREIGN KEY (id_osoby) REFERENCES osoby (id_osoby) ON DELETE CASCADE;
    

    There is no ALTER CONSTRAINT command. Drop and recreate the constraint in a single ALTER TABLE statement to avoid possible race conditions with concurrent write access.

    You need the privileges to do so, obviously. The operation takes an ACCESS EXCLUSIVE lock on table kontakty and a SHARE ROW EXCLUSIVE lock on table osoby.

    If you can’t ALTER the table, then deleting by hand (once) or by trigger BEFORE DELETE (every time) are the remaining options.

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

Sidebar

Related Questions

I would like to delete all the rows found by that query: SELECT cart_abandon.*
We have an application that logs using log4net. But we would like to delete
In PostgreSQL I have a query like the following which will delete 250k rows
I would like to delete all rows in tbl_license_user_alert when deleting either a license
I would like to delete the space between lines in a text file using
I would like to delete every double space in a file open in vim,
I would like to delete all the links in a quicklaunch in SP2010 with
I'm using : log_file = AWS::S3::Bucket.logs_for('downloads.idance.net').first I would then like to delete each file
I would like to run a timer for every 5 hours and delete the
I would like to bind an Image to some kind of control an delete

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.