ITADN

Preserve column order

#53ClosedUbunteous 创建于 2025-03-29
U
Ubunteouscommented
Hello. First of all, congratulations for creating this library. It is really convenient and has simplified my app quite a bit by replacing a few deps. I am just having an issue in pg2 v0.1.35 with the column order which does not match what I get with regular sql queries. For instance, if I work in a repl ``` clojure user> (require '[pg.core :as pg]) nil ``` Create a table ``` clojure user> (pg/query conn "create table issue ( name varchar(50) PRIMARY KEY UNIQUE, surname VARCHAR (50), age int )") {:command "CREATE TABLE"} ``` And fill it ``` clojure user> (pg/execute conn "insert into issue (name, surname, age) values ('Harry', 'Potter', 16), ('Hermione', 'Granger', 16)") {:inserted 2} ``` The output I will get with a SELECT statement won't respect both the table's default column order or the one I provide in the query (age, name, surname rather than name, surname, age) ``` clojure user> (pg/execute conn "select * from issue") [{:age 16, :name "Harry", :surname "Potter"} {:age 16, :name "Hermione", :surname "Granger"}] user> (pg/execute conn "select name, surname, age from issue") [{:age 16, :name "Harry", :surname "Potter"} {:age 16, :name "Hermione", :surname "Granger"}] ``` I can work around it by reordering the keys from the output but it is not ideal ```clojure user> (into [] (map #(select-keys % [:name :surname :age]) (pg/execute conn "select * from issue"))) [{:name "Harry", :surname "Potter", :age 16} {:name "Hermione", :surname "Granger", :age 16}] ``` Interestingly, using postgresql with the same query will give me the correct order ```sql SELECT * FROM issue; -- name, surname, age -- Harry, Potter, 16 -- Hermione, Granger, 16 ``` Could you please tell me if I missed something in the docs or if a fix could be done to correct this behavior?
关闭于 2025-04-01 7 条评论