---
title: "PostgreSQL: reject cross-entry revisions"
category: "code"
id: 5a3d669d-a9ba-4a25-9f7b-19582770dc4b
canonical: https://solvedlist.com/solutions/postgresql-reject-cross-entry-revisions
---

# PostgreSQL: reject cross-entry revisions

> In PostgreSQL, enforce rejection of cross-entry revision approvals and clear only the approval pointer upon revision deletion by referencing the unique pair (entry_id, id) and using a nullable pointer in the delete action.

## Question

How to enforce database rejection of cross-entry revision approval and clear only the approval pointer if the referenced revision is deleted in PostgreSQL?

## Solution

A foreign key on the revision ID proves existence only. Reference the unique pair (entry_id, id) using the parent row's ID and its optional approval pointer. In PostgreSQL 15+, list only the nullable pointer in the delete action. With plain SET NULL, the delete attempts to clear the parent primary key too and fails with SQLSTATE 23502.

```sql
CREATE TABLE entries (
    id integer PRIMARY KEY,
    approved_revision_id integer
);

CREATE TABLE entry_revisions (
    id integer PRIMARY KEY,
    entry_id integer NOT NULL REFERENCES entries(id),
    UNIQUE (entry_id, id)
);

ALTER TABLE entries
    ADD CONSTRAINT approved_revision_same_entry
    FOREIGN KEY (id, approved_revision_id)
    REFERENCES entry_revisions (entry_id, id)
    ON DELETE SET NULL (approved_revision_id);
```

A cross-entry assignment fails with SQLSTATE 23503. This constraint enforces ownership. Check review state and forward-only promotion in the approval transaction.

## Query directly via MCP

```text
POST https://solvedlist.com/mcp
Authorization: Bearer sl_live_…
Content-Type: application/json

{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"solvedlist_lookup","arguments":{"question":"…"}}}
```

Create an API key without an account:

```bash
curl -sS -X POST https://solvedlist.com/api/v1/keys -H 'Content-Type: application/json' -d '{"note":"agent"}'
```

More for agents: https://solvedlist.com/llms.txt and https://solvedlist.com/openapi.json
