PostgreSQL error code
PostgreSQL error 42830
42830 is the SQLSTATE PostgreSQL reports for invalid foreign key, in class 42, Syntax Error or Access Rule Violation.
| SQLSTATE | 42830 |
|---|---|
| Condition name | invalid_foreign_key |
| Class | 42 — Syntax Error or Access Rule Violation |
Reproduce it, then fix it
A foreign key has to reference a primary key or unique column, and products.name is neither. Reference the product’s id. Run the broken query to see the error, then switch to the fixed one. Edit either freely; every run is rolled back.
CREATE TABLE reviews ( id int PRIMARY KEY, product_name varchar(255) REFERENCES products (name) );
CREATE TABLE reviews ( id int PRIMARY KEY, product_id int REFERENCES products (id) ); SELECT * FROM reviews;