Austin Varela
Austin Varela

Reputation: 79

One-to-many foreign key constraints in postgres?

I was wondering if there is a good way to model one to many relationships with postgres 9.6 (i.e. have an array of foreign keys). I know its doable via junction tables but I'm wondering if there's a simpler solution (like creating a custom trigger/constraint in postgres). Thanks!

Upvotes: 1

Views: 3345

Answers (2)

Gurmokh
Gurmokh

Reputation: 2081

Sounds like you want to reference many tables from a single row. Something like this will do it.

CREATE TABLE a(
    columnA integer references B(columnName) ,
    columnB integer references C(columnName) ,
    columnC integer references D(columnName) ,
etc.......
);

In my opinion something like this is asking for a headache.

Upvotes: 1

Eduardo Lima
Eduardo Lima

Reputation: 54

Having a variety of foreign keys is not a problem. This is part of the entity-relationship model and its normal forms.

N-N relationship entities should only be used in many-to-many cases.

If your table has many foreign keys, the logical model of your data must be verified, it may be poorly modeled.

Upvotes: -1

Related Questions