Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bO450-0001gC-Jk for pgsql-jdbc@arkaria.postgresql.org; Fri, 15 Jul 2016 14:27:02 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bO450-00035t-5m for pgsql-jdbc@arkaria.postgresql.org; Fri, 15 Jul 2016 14:27:02 +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_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bO3o6-0008Ty-Lo for pgsql-jdbc@postgresql.org; Fri, 15 Jul 2016 14:09:34 +0000 Received: from mail.list-group.com ([62.48.50.226] helo=mail2.list-group.com) by magus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1bO3o2-00036b-J8 for pgsql-jdbc@postgresql.org; Fri, 15 Jul 2016 14:09:33 +0000 Received: from host210-56-static.60-79-b.business.telecomitalia.it ([79.60.56.210] helo=[192.168.232.203]) by mail2.list-group.com with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.69) (envelope-from ) id 1bO3nz-00033g-7H; Fri, 15 Jul 2016 16:09:27 +0200 To: pgsql-jdbc@postgresql.org From: Andrea Bergia Subject: Possible bug related to primary keys autogeneration Message-ID: <56bd3f97-c3e5-4ac8-daf9-ed6e73f48014@list-group.com> Date: Fri, 15 Jul 2016 16:09:37 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -3.2 (---) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-jdbc Precedence: bulk Sender: pgsql-jdbc-owner@postgresql.org Hello, I have an issue regarding the retrieval of autogenerated keys using JDBC. I am running both the client and the server on windows; the client using the JDBC driver 9.4.1208 and the server has version "PostgreSQL 9.5.3 on x86_64-pc-mingw64, compiled by gcc.exe (Rev5, Built by MSYS2 project) 4.9.2, 64-bit". I have the following schema: CREATE TABLE Documents ( id SERIAL, name VARCHAR(100), CONSTRAINT PK_Documents PRIMARY KEY (id) ); and the following Java code: try (Connection connection = DriverManager.getConnection("jdbc:postgresql://localhost/sampledb", "sampledb", "")) { connection.setAutoCommit(false); try (PreparedStatement ps = connection.prepareStatement("INSERT INTO Documents (name) VALUES (?)", new String[]{"ID"})) { ps.setString(1, "DocName"); ps.executeUpdate(); try (ResultSet generatedKeys = ps.getGeneratedKeys()) { if (!generatedKeys.next()) { throw new RuntimeException("Should have been able to retrieve the generated keys"); } int generatedKey = generatedKeys.getInt(1); System.out.println("The generated key is " + generatedKey); } } } I get the following exception: Exception in thread "main" org.postgresql.util.PSQLException: ERROR: column "ID" does not exist Debugging a bit, the issue seems to be in PgConnection.java, line 1641: since "escape" is set to "true", the driver is adding RETURNING "ID", quoting the column name. Since the column name's case is different between the CREATE TABLE statement and the code, PostgreSQL generates an exception. In fact, if I use new String[]{"id"}, the program works. I'm wondering whether this can be classified as a bug, or is intentional. The driver doesn't quote column names generally; it seems to me that this behavior is a bit annoying. However, I have no idea what the implications of removing the escaping would be. I do have a workaround for the moment, but I would like to know whether this is something that can be changed or not. I have uploaded a complete, minimal project which includes the code above at https://github.com/andreabergia/psql-jdbc-possible-bug-report. Thanks for your time, and for your excellent product. -- Andrea Bergia List S.p.A. -- Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-jdbc