Re: Table with Field Serial - Problem

From: Yostin Vargas <yostinv(at)gmail(dot)com>
To: Adrian Klaver <adrian(dot)klaver(at)gmail(dot)com>
Cc: David Johnston <polobo(at)yahoo(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Table with Field Serial - Problem
Date: 2013-10-31 18:12:13
Message-ID: CAPMdYjBGLURuD_ExuAUHFSimKT07zLcj8CJ-9=GRZjBgXrvq8w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

i really dont need a number generator, only a unique PK. but i want that
this PK be generate automatically

for example i have a Category calling Computer in English but i have the
same Category in Spanish (Computadora) i assigned the ID->1 for both

So if i put the Pk ID in the table2 number i have error for unique
violation when i want INSERT another name in a diferent language for the
same category

For that reason i declare ID in the table2 like a FK from ID in the table1

2013/10/31 Adrian Klaver <adrian(dot)klaver(at)gmail(dot)com>

> On 10/31/2013 09:32 AM, David Johnston wrote:
>
>> Adrian Klaver-3 wrote
>>
>>> Table1
>>>> Column | Type | Modifiers
>>>> ----------+-------------------**__+---------------------------**
>>>> --__--------------------------**----__--
>>>> id | integer | not null default
>>>> nextval('test_table_id_fld___**seq'::regclass)
>>>>
>>>>
>>>> Table2
>>>> Column | Type | related
>>>> ----------+-------------------**__+---------------------------**
>>>> --__--------------------------**----__--
>>>> id_table1 | integer | FK of Table1.id
>>>> id_lang | integer | FK of lang.id
>>>> &lt;http://lang.id&gt;
>>>> name | varchar
>>>>
>>>>
>> The PK for table 2 is composite: the serial key from table 1 + the
>> language
>> id. The table 1 id has to be able to repeat since the same "entity" needs
>> multiple translations. Using a serial on table 2 is also possible but a
>> separate issue and probably not worth adding since you need a unique index
>> on (id_table1, id_lang) regardless.
>>
>> The question is why isn't there some kind of identifier on table 1 that
>> gives you some idea of what the id/table record is for?
>>
>
> Exactly the id_table1 FK has no context, it is just a number generator, so
> why make it separate? If want to just generate numbers why not just use the
> sequence directly?
>
>
>> David J.
>>
>>
>>
>
>
> --
> Adrian Klaver
> adrian(dot)klaver(at)gmail(dot)com
>
>
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/**mailpref/pgsql-general<http://www.postgresql.org/mailpref/pgsql-general>
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2013-10-31 18:22:29 Re: Table with Field Serial - Problem
Previous Message hubert depesz lubaczewski 2013-10-31 17:42:20 Re: Explanantion on pgbouncer please