Re: arrays of foreign keys

From: "Albe Laurenz" <laurenz(dot)albe(at)wien(dot)gv(dot)at>
To: "Max *EXTERN*" <mlavenne(at)gmail(dot)com>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: arrays of foreign keys
Date: 2007-09-10 13:08:38
Message-ID: D960CB61B694CF459DCFB4B0128514C22FBC20@exadv11.host.magwien.gv.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Max wrote:
> I am trying to create a table with an array containing foreign keys.
> I've searched through the documentation and couldn't find a way to do
> so.
>
> Is this something that one can do?
>
> Basically, I have two tables:
>
> create table user (
> user_id serial,
> login varchar(50) primary key,
> tags integer[]-- this is where the problem lies
> );
>
> create table tag (
> tag_id serial,
> name varchar(50) primary key
> );
>
> I would like the user.tags field to be a list of foreign keys (tag_ids
> specifically).

Fortunately, this is not possible.

> I know I can solve my problem with the following table:
>
> create table user_tag (
> user_id integer,
> tag_id integer,
> foreign key (user_id) references table user(user_id) on delete
> cascade,
> foreign key (tag_id) references table tag(tag_id) on delete cascade,
> primary key (user_id,tag_id)
> );
>
> But I would really like to avoid doing that.

Why?

That is the correct and efficient solution of your problem.

Go for it!

Yours,
Laurenz Albe

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2007-09-10 13:39:46 Re: Checking is TSearch2 query is valid
Previous Message Ron Johnson 2007-09-10 13:04:20 Re: arrays of foreign keys