pg_graphql icon indicating copy to clipboard operation
pg_graphql copied to clipboard

Cannot filter queries on columns of type CHAR

Open alex-ketch opened this issue 3 years ago • 1 comments

Describe the bug Attempting to perform a query over columns of type CHAR / CHAR(n) returns empty results. Changing field type to VARCHAR fixes the filtering.

To Reproduce Steps to reproduce the behavior:

  1. Create a table schema with a field of type CHAR, e.g. "iso2" CHAR(2) NOT NULL UNIQUE.
  2. Populate database with data
  3. Make a query to verify that data is present.
  4. Attempt to add filter to above query
  5. See empty results

Sample table schema

CREATE TABLE IF NOT EXISTS states (
  "name" VARCHAR(255) NOT NULL,
  "iso2" CHAR(2) NOT NULL UNIQUE,
  PRIMARY KEY ("iso2")
);

Expected behavior Matching rows are returned.


Screenshots

Verify that data is present Screenshot 2022-07-09 at 23 47 48@2x

Empty results when attempting to filter above data Screenshot 2022-07-09 at 23 55 10@2x

alex-ketch avatar Jul 10 '22 04:07 alex-ketch

that you for reporting, I was able to reproduce the issue

When casting inputs from GraphQL types to PostgreSQL types pg type modifiers are ignored (except for arrays). That is not usually an issue because e.g. varchar can handle anything that varchar(200) can.

In the char case, the casting rule is unusual

select 'GB'::char -- 'G'

to resolve this, we'll need to retain the modifiers when casting.

Your solution of using a varchar is a good choice

olirice avatar Jul 11 '22 13:07 olirice