Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iillt-0007MH-FU for pgsql-general@arkaria.postgresql.org; Sat, 21 Dec 2019 20:54:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iilku-0003vm-2Y for pgsql-general@arkaria.postgresql.org; Sat, 21 Dec 2019 20:53:44 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iilks-0003vf-5T for pgsql-general@lists.postgresql.org; Sat, 21 Dec 2019 20:53:43 +0000 Received: from wout4-smtp.messagingengine.com ([64.147.123.20]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1iilkn-0002E0-7U for pgsql-general@lists.postgresql.org; Sat, 21 Dec 2019 20:53:41 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id 7A0A54D7; Sat, 21 Dec 2019 15:53:33 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Sat, 21 Dec 2019 15:53:33 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=9 pvWWm3oaw5EBhBGoTXd/ZPSET/FzdRzoWOWmgwSx1I=; b=PJkRSZYs+/Mldp+Hp TGxo7cmo+TBfOgSBYZH7Pj68TDiGYM+s2cQDWVpwC2hRY7g+PGKb6YH2wT+FfpWw Gwb1ZbrX2A5CHG1wFuFulNA/9vZLt8gZC5TYZsQn9ToYC8yqCBhDSziSXWeOTurP jaiV1M3m4PQ45TXJv7pXjlNOSqGmqnU4gnGtSUn47e/7/NZSt2b5tSF5FuXgWA0J qL1aX3I6B5qsEXEQYb6f0EGQLcRVn+8BqiyBah8qrdX7NKOHPPwlkMO/qjv5lwk5 zdGQOPjNC+872MqM1++iC6uQS0yoXlWRYAMzUsPu22a1GVmZx6sJ+ds0Z6fFcPFZ EpVYw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=9pvWWm3oaw5EBhBGoTXd/ZPSET/FzdRzoWOWmgwSx 1I=; b=coXBbWTo8HpGOHrLFNR7e/5gjCrojiCZE51st8zE0ukTIxfVfu+ITmHKq BvMUZgpVKg7GL8rIrpEjgMPthIx3hjEw4VAKIl5m7L664Hrjeh9mab2t8mzEypKB 6vCB8FCLML8fxsMfdiqumS5vZ9Brm2dmpIW5+6wdhrUFvdWOPLiIZxZxdi5N7wN1 qBMadqzCuw4ZSiv1MSbeJ71HRpplXc/6Ed1oQb8Kv7nA0YGrESoddwkmTDenmoxs E8LZc+l373TkX2Mu3D6Z3qtd6WyuC9cXlthcWOEBjJzJGLkiUl7b158rHjzllF8H ysTDNG6fIcLuQh1OdkUHbh6QO5SRw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrvdduhedgudegtdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughr ihgrnhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtg homheqnecuffhomhgrihhnpehpohhsthhgrhgvshhqlhdrohhrghdpohhffhhitggvrdgt ohhmnecukfhppeejuddrvdduvddruddvledrudelfeenucfrrghrrghmpehmrghilhhfrh homheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomhenucevlhhushht vghrufhiiigvpedt X-ME-Proxy: Received: from [192.168.1.2] (unknown [71.212.129.193]) by mail.messagingengine.com (Postfix) with ESMTPA id 6BCE730607B0; Sat, 21 Dec 2019 15:53:32 -0500 (EST) Subject: Re: Semi-unable to add new records to table--primary key needed? To: "Boylan, Ross" , Ron , "pgsql-general@lists.postgresql.org" References: <9c113278-3593-92dc-fbe5-7e3e0018218a@gmail.com> From: Adrian Klaver Message-ID: <66c14222-ac25-e9f5-7a29-3ccebb26e987@aklaver.com> Date: Sat, 21 Dec 2019 12:53:31 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 12/21/19 12:21 PM, Boylan, Ross wrote: > Thank you for the confirmation on the need for a primary key.  I suspected that, since the GUI needs an easy way to refer to a particular row.  I think I saw such a restriction in the Qt documentation on a different project (just to be > clear: no Qt involved in this one--just more evidence this is a pretty general pattern). > > It's interesting that Access does not behave this way if the backend is an Access (.mdb) file; I suppose it knows  enough to get some unique identifier in that case.  The https://support.office.com/en-us/article/Add-or-change-a-table-s-primary-key-in-Access-07b4a84b-0063-4d56-8b00-65f2975e4379 "When you create a new table in Datasheet view, Access automatically creates a primary key for you and assigns it a field name of "ID" and the AutoNumber data type." difference is not that the backend table in Access has a primary key; the reason the PG table lacked a primary key was that the Access table from which it was migrated didn't have one. My guess is the migration process missed that aliquotid was the PK. > > I've read more about sequences, and it seems mine just aren't in sync with the data, and that using sequences requires some care.  Easier to discuss with an example. > CREATE TABLE tx ( >    xid serial, >    a int8 > ); > This will produce a sequence tx_xid_seq. > > If I do > INSERT INTO tx VALUES (3, 4); > the sequence doesn't know about it.  So if I later create a default value with > INSERT INTO tx (a) VALUES (7); > or > INSERT INTO tx VALUES (DEFAULT, 9); > I'll just get the next value in the sequence. > > The 3rd time I use the default value it will return 3, same as the record already there.  If there is a PRIMARY KEY (or UNIQUE) constraint on xid the insertion will fail. > > So.... >   1. When I migrate data, as I have done, I should ensure that the sequences are at safe values.  The obvious way to do that would be setval using the max of the values in the data. > 2. In operations, the program needs to either be consistent about getting id values from the default, or be very careful.  Since I'm using an  inherited application, I need to check. > > When I migrate the data I do NOT want to use the sequence to generate the identifiers, since that will trash  the referential integrity of the data That rather depends on what you using to migrate the data. Postgres pg_dump will not do that if you dump the table. > > Here's the transcript of my tests.  BTW, why is log_cnt jumping to 32 on the sequence? log_cnt is an internal counter: https://www.postgresql.org/message-id/7487.1049476267%40sss.pgh.pa.us and does not really apply to the sequence value. The value is determined by last_value and is_called. For more information see: https://www.postgresql.org/docs/11/sql-createsequence.html You skipped over the sequence by starting with a value of 2 and then you went back and used the sequence, so when it got to 2 it threw a duplicate error which is correct as the field was a PK. Best practice on a sequence is to let it run on its own and not try to override it. If you do then you will need to familiarize yourself with the functions here: https://www.postgresql.org/docs/11/functions-sequence.html > --Now with a PRIMARY KEY constraint > testNTB=> CREATE TABLE ty ( > testNTB(> yid serial, > testNTB(> a int8, > testNTB(> PRIMARY KEY (yid)); > CREATE TABLE > testNTB=> INSERT INTO ty VALUES (2, 10); > INSERT 0 1 > testNTB=> select * from ty; >  yid | a > -----+---- >    2 | 10 > (1 row) > > > testNTB=> select * from ty_yid_seq; >  last_value | log_cnt | is_called > ------------+---------+----------- >           1 |       0 | f > (1 row) > > > testNTB=> INSERT INTO ty VALUES (DEFAULT, 20); > INSERT 0 1 > testNTB=> select * from ty; >  yid | a > -----+---- >    2 | 10 >    1 | 20 > (2 rows) > > > testNTB=> select * from ty_yid_seq; >  last_value | log_cnt | is_called > ------------+---------+----------- >           1 |      32 | t > (1 row) > > > testNTB=> INSERT INTO ty VALUES (DEFAULT, 30); > ERROR:  duplicate key value violates unique constraint "ty_pkey" > DETAIL:  Key (yid)=(2) already exists. > > -- Adrian Klaver adrian.klaver@aklaver.com