Skip to content

Discard columns with null values #1601

Closed
@jsommr

Description

@jsommr

Say I have a polymorphic table contact that can be either person or company (each in their own table):

create type contact_type as enum (
  'company',
  'person'
);

create table contact (
  contact_id   uuid         primary key default uuid_time_nextval(),
  contact_type contact_type not null
);

create table company (
  company_id   uuid primary key references contact,
  company_name text not null
);

create table person (
  person_id      uuid primary key references contact,
  preferred_name text not null,
  email          text not null
);

With insert triggers on company and person ensuring a contact with the same id is inserted as well.

We create a view that combines them:

create view v1.contact as
  select 
    contact.*,
    c.company_name,
    p.preferred_name,
    p.email
  from contact
  left join company c on contact_id = company_id
  left join person p on contact_id = person_id;

With a little sample data, requesting this via /contact?select=* returns:

[
  {
    "contact_id": "546137e0-cda5-4e24-b684-94d030bfcf42",
    "contact_type": "company",
    "company_name": "Nerfpops Incorporated",
    "preferred_name": null,
    "email": null
  },
  {
    "contact_id": "54772635-7feb-438a-b4ee-251e23684e51",
    "contact_type": "person",
    "company_name": null,
    "preferred_name": "Nerfpops",
    "email": "[email protected]"
  }
]

Is there any way to tell PostgREST not to include fields that are null in the JSON response? Even if I explicitly asked for the field, I'd still prefer not to have it in the response.

Metadata

Metadata

Assignees

No one assigned

    Labels

    difficulty: mediumHaskell task involving PostgreSQL IOenhancementa feature, ready for implementation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions