226

I am attempting to create a DB for my app and one thing I'd like to find the best way of doing is creating a one-to-many relationship between my Users and Items tables.

I know I can make a third table, ReviewedItems, and have the columns be a User id and an Item id, but I'd like to know if it's possible to make a column in Users, let's say reviewedItems, which is an integer array containing foreign keys to Items that the User has reviewed.

If PostgreSQL can do this, please let me know! If not, I'll just go down my third table route.

1

2 Answers 2

196

Please note that this answer describes a feature that has not yet been implemented (Dec 2024).

There is some abandoned work on that, see: https://commitfest.postgresql.org/17/1252/

The patch would have (once complete) allowed

CREATE TABLE PKTABLEFORARRAY (
    ptest1 float8 PRIMARY KEY,
    ptest2 text
);
CREATE TABLE FKTABLEFORARRAY (
    ftest1 int[],
    FOREIGN KEY (EACH ELEMENT OF ftest1) REFERENCES PKTABLEFORARRAY,
    ftest2 int
);

However, there seems to be not an active work, with no activity in 2020s.

Sign up to request clarification or add additional context in comments.

12 Comments

very HOT indeed... CREATE TABLE FKTABLEFORARRAY ( ftest1 int[], FOREIGN KEY (EACH ELEMENT OF ftest1) REFERENCES PKTABLEFORARRAY, ftest2 int )
shame, it doesn't seems to have been included :(
Per the patch email thread, as of July 2021 it appears that this "still requires some major surgery." So we could still have a while.
@JohnO It would definitely be awesome, but upvotes are really supposed to be for useful answers and this answer isn't currently useful because the behavior it describes is still not implemented. As of right now, there is only one truly correct answer to the question: "you can't do it". (I upvoted this one too, but that probably wasn't really the right thing to do.)
|
179

No, this is not possible.

PostgreSQL is a relational DBMS, operating most efficiently on properly normalized data models. Arrays are not relational data structures - by definition they are sets - and while the SQL standard supports defining foreign keys on array elements, PostgreSQL currently does not support it. There is an (dormant? no activity on commitfest since February 2021) effort to implement this - see this answer to this same question - so the functionality might one day be supported.

For the time being you can, however, build a perfectly fine database with array elements linking to primary keys in other tables. Those array elements, however, can not be declared to be foreign keys and the DBMS will therefore not maintain referential integrity. Using an appropriate set of triggers (both on the referenced and referencing tables, as a change in either would have to trigger a check and possible update on the other) one would in principle be able to implement referential integrity over the array elements but the performance is unlikely to be stellar (because indexes would not be used, for instance).

8 Comments

You can define a constraint trigger that would check that. But I'm not sure if it works reliably in all cases.
@LuanHuynh: I don't recall the (technical) details, but the last time this was discussed on the mailing list someone mentioned that a corresponding constraint trigger might not catch all cases - but that might no longer be true
@a_horse_with_no_name can you post an answer with an example of such a constraint?
workaround / re-design: having a separate many-to-many table
Apparently, sql:2016 does support this behavior. Unfortunately the standard isn't freely available, but you can see it referenced in both Microsoft's documentation and PostgreSQL's list of unsupported standard features.
|

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.