yglodt
yglodt

Reputation: 14551

Ordering by Postgres hstore attribute not working as expected

I use Postgres 10.4 and I get a strange ordering behaviour in a simple query.

My table is defined like this:

CREATE TABLE country (
    id           varchar(2)   NOT NULL PRIMARY KEY,
    order_by     smallint,
    app_locale   boolean      DEFAULT false NOT NULL,
    country      boolean      DEFAULT false NOT NULL,
    lang         boolean      DEFAULT false NOT NULL,
    nationality  boolean      DEFAULT false NOT NULL,
    c_name       hstore,
    l_name       hstore,
    n_name       hstore,
    active       boolean      DEFAULT false NOT NULL,
    language     varchar(6)
);

This simple query should return the countries ordered by the nationality name in English language:

select id, c_name
from country c
where active = true
and   c.nationality = true
and   id in ('de','fr','be','lu','kz','af','ad') -- limit the result a bit
order by c.n_name -> 'en'

The unexpected result is:

id  c_name
be  "de"=>"Belgien", "lb"=>"Belsch", "en"=>"Belgium", "fr"=>"Belgique"
fr  "de"=>"Frankreich", "lb"=>"Frankräich", "en"=>"France", "fr"=>"France"
de  "de"=>"Deutschland", "lb"=>"Däitschland", "en"=>"Germany", "fr"=>"Allemagne"
lu  "de"=>"Luxemburg", "lb"=>"Lëtzebuerg", "en"=>"Luxembourg", "fr"=>"Luxembourg"
ad  "en"=>"Andorra"
kz  "en"=>"Kazakhstan"
af  "de"=>"Afghanistan", "en"=>"Afghanistan", "fr"=>"Afghanistan"

What is the problem here?

Upvotes: 1

Views: 492

Answers (1)

klin
klin

Reputation: 121824

You used the wrong column in order by:

select id, c_name
from country c
where active = true
and c.nationality = true
and id in ('de','fr','be','lu','kz','af','ad')
order by c_name -> 'en'

Upvotes: 4

Related Questions