Greenstand / treetracker-admin-api

Legacy API microservice for the admin panel
GNU Affero General Public License v3.0
31 stars 81 forks source link

Filtering trees by Any Tag Set returns duplicate records #609

Closed nmcharlton closed 2 years ago

nmcharlton commented 2 years ago

Passing the following query to GET /trees returns duplicate records:

filter: "where":{"approved":true,"active":true,"tagId":"0"}

Trees (captures) appear to be included once per tag that they have applied, e.g., a tree with three tags appears three times in the results.

nmcharlton commented 2 years ago

@bstetzer32 Would you mind looking at this one?

nmcharlton commented 2 years ago

Fix reverted – see #612

ZavenArra commented 2 years ago

I think this issue requires the specification of which column names( necessary to return and distinct)

SELECT DISTINCT id, (other column names) FROM ...

Though actually, it seems to me that the below solution (which was reverted) should work.

        const columnNames = this.dataSource.connector
          .buildColumnNames('Trees', filter)
          .replace('"id"', 'trees.id as "id"');

const selectStmt = `SELECT DISTINCT ${columnNames} from trees ${this.getTreeTagJoinClause(
          tagId,
        )}`;

OR use of the DISTINCT ON clause.

https://www.postgresql.org/docs/current/sql-select.html https://www.geekytidbits.com/postgres-distinct-on/