X-Original-To: pgsql-odbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id BA7EAD1B441 for ; Thu, 8 Apr 2004 12:21:37 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 79291-04 for ; Thu, 8 Apr 2004 12:21:34 -0300 (ADT) Received: from smtp.covadmail.net (mx07.covadmail.net [63.65.120.67]) by svr1.postgresql.org (Postfix) with SMTP id 2B2D6D1D903 for ; Thu, 8 Apr 2004 12:21:33 -0300 (ADT) Received: (covad.net 5052 invoked from network); 8 Apr 2004 15:21:26 -0000 Received: from unknown (HELO covad.net) (rncombs@68.165.42.132) by sun-qmail16 with SMTP; 8 Apr 2004 15:21:26 -0000 Message-ID: <40756DF7.5010509@covad.net> Date: Thu, 08 Apr 2004 10:21:27 -0500 From: Richard Combs User-Agent: Mozilla Thunderbird 0.5 (Windows/20040207) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Richard Huxton Cc: Philippe Lang , pgsql-odbc@postgresql.org Subject: Re: Access - ODBC - serial problem... References: <6C0CF58A187DA5479245E0830AF84F420802A0@poweredge.attiksystem.ch> <200404081448.53157.dev@archonet.com> In-Reply-To: <200404081448.53157.dev@archonet.com> Content-Type: multipart/alternative; boundary="------------010705010507080803070001" X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200404/63 X-Sequence-Number: 3958 This is a multi-part message in MIME format. --------------010705010507080803070001 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit The problem, as I see it, is that you are creating a table that will have multiple instances of the same value for code >CREATE TABLE public.test >( > id serial PRIMARY KEY, > code int4, > f1 int4, > f2 varchar(50), > f3 text >) WITHOUT OIDS; > Your return for the query 'select id where code = 20' will return a result set, not a singleton response. This will not tell you what the latest value of id was, only all values. If you normalize your table on code (i.e., make code a unique value) you will always get a singleton response to your query. However, you will get an error that you will have to trap, when you try to insert duplicate values into the table. My solution, and I work in Delphi, not Access, so I can't tell you how access will work with this, is to create a function in postgres that inserts your values and returns currval(''id''). Currval is always the id you just inserted into the table. You then call the function as a stored procedure (or whatever its equivalent is in Access), with the proper parameters, and you get the id you just inserted to work with. Check the docs for proper syntax on writing the function and using currval. HTH Richard Huxton wrote: >On Thursday 08 April 2004 13:23, Philippe Lang wrote: > > >>With the table described below, imagine I do, from the client: >> >>insert into test (code) VALUES (20); >> >>How does the client know the id that has been given to the record? With >>ethereal, I could see Access fetches the id by doing a >> >>select id from test where code = 20" >> >>Of course, another record has the same code, and the wrong id is being >>fetched back. This explains what I have noticed, and that is explained >>below... >> >> > >Do you not have a primary key on your table? > > > > --------------010705010507080803070001 Content-Type: text/html; charset=us-ascii Content-Transfer-Encoding: 7bit The problem, as I see it, is that you are creating a table that will have multiple instances of the same value for code
CREATE TABLE public.test
(
  id serial PRIMARY KEY,
  code int4,
  f1 int4,
  f2 varchar(50),
  f3 text
) WITHOUT OIDS;
Your return for the query 'select id where code = 20' will return a result set, not a singleton response.  This will not tell you what the latest value of id was, only all values.  If you normalize your table on code (i.e., make code a unique value) you will always get a singleton response to your query.  However, you will get an error that you will have to trap, when you try to insert duplicate values into the table.

My solution, and I work in Delphi, not Access, so I can't tell you how access will work with this, is to create a function in postgres that inserts your values and returns currval(''id'').  Currval is always the id you just inserted into the table.  You then call the function as a stored procedure (or whatever its equivalent is in Access), with the proper parameters, and you get the id you just inserted to work with.

Check the docs for proper syntax on writing the function and using currval.

HTH

Richard Huxton wrote:
On Thursday 08 April 2004 13:23, Philippe Lang wrote:
  
With the table described below, imagine I do, from the client:

insert into test (code) VALUES (20);

How does the client know the id that has been given to the record? With
ethereal, I could see Access fetches the id by doing a

select id from test where code = 20"

Of course, another record has the same code, and the wrong id is being
fetched back. This explains what I have noticed, and that is explained
below...
    

Do you not have a primary key on your table?


  

--------------010705010507080803070001--