8

When i try to delete rows from a table, it does not work. I used the standard DELETE FROM statement, using tag_id=1 The first statement is returning Success, so i expect that the rows are deleted. However, the rows are not deleted. And i can't seem to figure out why.

  • What could be the cause of this?

The image below has the specific query statements, and the return result.

enter image description here

3
  • Do you have delete privilige on the table? Commented Nov 13, 2019 at 10:33
  • 1
    When you do the delete, does it tell you how many rows were affected? Commented Nov 13, 2019 at 10:59
  • Is autocommit on? Is it possible that you deleted, rolled back, and then selected? Commented Nov 13, 2019 at 13:14

3 Answers 3

4

There are several possibilities:

  • There is a trigger on the table that prevents the rows from being deleted.

    Have a look at

    \d project_tag
    
  • Your index is corrupted. Try

    REINDEX TABLE project_tag;
    
Sign up to request clarification or add additional context in comments.

1 Comment

Notably for triggers, ensure that you are not returning NULL from the DELETE, which is the condition for it to skip the operation: postgresql.org/docs/15/trigger-definition.html I did that with an accidental RETURN NEW instead of RETURN OLD.
1

My issue was different (in that the DELETE appeared to have worked), but this question was the number one search result and phrased exactly like my issue. I want to say:

AUTOCOMMIT

Some DB connections have autocommit as an option during their connection profile. Checking that solved my issue.

1 Comment

Agree, am using SQLAlchemy python and I set the autocommit=False, hence, I have to call session.commit() to make it executed against the database.
0

If none of the other answers work, I would recommend trying explain on the delete query. In my case it turned out to be a faulty RLS policy (more details below). The output of explain showed a suspicious Filter: (false AND (id='...'::uuid)), where, the false was being introduced by the RLS policy.

Details about RLS fault: I had included only a single policy for delete and it was a restrictive policy. After carefully reading the RLS docs, I found out that there must be at least one permissive policy for the restrictive policies to make sense. If not, then no records will be accessible.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.