#1941·bookshelf

Using withRelated fails when related table has json column in PostgreSQL

Author: dbrownllCreated Jan 23, 2019Updated Jul 7, 2022
LabelsbugPR pleaserelations

Introduction

Using withRelated in a query for a related table that has a JSON column causes a "could not identify an equality operator for type json" error to be thrown when used with PostgresSQL. Prior to v0.14.0 related tables used "select", but from 0.14.0 it now uses "select distinct".

Issue Description

Using .query(function(qb) { qb.where('id', 'in', applicantIds); }) .fetchAll({ withRelated: ['completionDates'] })

where completionDates maps to the completion_dates table with a hasOne relation, and the initial table has a hasOne relation to the completion_dates table, the following SQL queries are produced:

Prior to 0.14.0: select "completion_dates".*

Post 0.14.0: select distinct "completion_dates".*

I believe it was caused by "Fix performance of including relationships"

Steps to reproduce issue

Create two tables, where the second table has the json data-type. The second table has a foreign key relating to the first table, and maps to it with the bookshelf belongsTo relation. The first table has a hasOne relation for the second table. Then perform a query on the first table using the above code e.g.

.query(function(qb) { qb.where('id', 'in', applicantIds); }) .fetchAll({ withRelated: ['completionDates'] })

Expected behaviour

Expected a select completion_dates.* query to be performed

Actual behaviour

A select distinct completion_dates.* query was performed