pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

logical expressions in views sometimes do not populate

Open whoward opened this issue 1 year ago • 1 comments

Continuing on with #59 this one is pretty hard to describe but I have managed to pare it down to a basic example. In this fairly contrived example (you'll have to trust this is distilled from a real example that actually makes sense) we can see that pgschema does not correctly replicate the view definition. Here is the database definition:

create table some_table (
  id serial primary key,
  type varchar(20),
  value varchar(20)
);

create view some_view as
  select
    id as entity_id,
    type,
    case when type in ('type_a', 'type_b') then value else null end as value
  from some_table
  order by id, type;

When we run pgschema dump on this database we get the following output:

--
-- pgschema database dump
--

-- Dumped from database version PostgreSQL 17.0
-- Dumped by pgschema version 1.3.0


--
-- Name: some_table; Type: TABLE; Schema: -; Owner: -
--

CREATE TABLE IF NOT EXISTS some_table (
    id SERIAL PRIMARY KEY,
    type varchar(20),
    value varchar(20)
);

--
-- Name: some_view; Type: VIEW; Schema: -; Owner: -
--

CREATE OR REPLACE VIEW some_view AS
 SELECT
    id AS entity_id,
    type,
    CASE WHEN type::text = ::text THEN value ELSE NULL::pg_catalog.varchar END AS value
   FROM some_table
  ORDER BY entity_id, type;

You can see the CASE WHEN type::text = ::text fragment is syntactically incorrect. I'm not sure exactly what causes this but it has something to do with the combination of the ORDER BY clause and the id AS entity_id in the query projection.

whoward avatar Oct 14 '25 20:10 whoward

just tested this change and it works great, thanks for taking care of that!

whoward avatar Oct 16 '25 15:10 whoward