Skip to content

Commit 8ec649e

Browse files
committed
fix: add modelName to query to avoid ambiguous fields
1 parent c98caeb commit 8ec649e

3 files changed

Lines changed: 34 additions & 7 deletions

File tree

src/js/buildFilterQuery.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ export function buildFilterQuery(selectStmt, params) {
2828

2929
const whereObjClause = connector._buildWhere(modelName, safeWhere);
3030

31+
//add the modelName to each requested field to avoid ambiguity in more complex queries
32+
console.log('whereObjClause.sql -------> ', whereObjClause.sql);
33+
34+
const tableName =
35+
modelName.toLowerCase() === 'planterregistration'
36+
? 'planter_registrations'
37+
: modelName.toLowerCase();
38+
const newQueryFields = whereObjClause.sql
39+
.replace(/"/, `"${tableName}.`)
40+
.replace(/AND "/gi, `AND "${tableName}.`)
41+
.replace(/"/g, '');
42+
43+
whereObjClause.sql = newQueryFields;
44+
3145
if (whereObjClause.sql) {
3246
const hasWhere = /WHERE(?![^(]*\))/i.test(selectStmt);
3347
query.sql += ` ${hasWhere ? 'AND' : 'WHERE'} ${whereObjClause.sql}`;
@@ -47,5 +61,7 @@ export function buildFilterQuery(selectStmt, params) {
4761
}
4862
}
4963

64+
console.log('QUERY ---------', query);
65+
5066
return query;
5167
}

src/mixins/utils.repository-mixin.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ export function UtilsRepositoryMixin<
8080
const planterIds = await this.getNonOrganizationPlanterIds();
8181
// if planter repository request
8282
if (model === 'planter') {
83+
// return {
84+
// and: [
85+
// { organizationId: null },
86+
// { 'planter.id': { inq: planterIds } },
87+
// ],
88+
// };
89+
// could just do this if we didn't need to use 'planter.id' to avoid errors with adding other filters
8390
return { id: { inq: planterIds } };
8491
} else {
8592
// if trees or other repository request
@@ -102,7 +109,7 @@ export function UtilsRepositoryMixin<
102109
return {
103110
or: [
104111
{ organizationId: { inq: entityIds } },
105-
{ id: { inq: planterIds } },
112+
{ 'planter.id': { inq: planterIds } },
106113
],
107114
};
108115
} else {

src/repositories/planter.repository.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { Planter, PlanterRelations, PlanterRegistration } from '../models';
1212
import { TreetrackerDataSource } from '../datasources';
1313
import { PlanterRegistrationRepository } from './planterRegistration.repository';
1414
import { UtilsRepositoryMixin } from '../mixins/utils.repository-mixin';
15-
import expect from 'expect-runtime';
15+
// import expect from 'expect-runtime';
1616
import { buildFilterQuery } from '../js/buildFilterQuery';
1717
import { utils } from '../js/utils';
1818

@@ -70,17 +70,22 @@ export class PlanterRepository extends UtilsRepositoryMixin<
7070

7171
try {
7272
if (this.dataSource.connector) {
73-
const columnNames = this.dataSource.connector
74-
.buildColumnNames('Planter', filter)
75-
.replace('"id"', 'planter.id as "id"');
73+
// const columnNames = this.dataSource.connector
74+
// .buildColumnNames('Planter', filter)
75+
// .replace('"id"', 'planter.id as "id"')
76+
// .replace('"first_name"', 'planter.first_name as "first_name"')
77+
// .replace('"last_name"', 'planter.last_name as "last_name"')
78+
// .replace('"email"', 'planter.email as "email"')
79+
// .replace('"organization"', 'planter.organization as "organization"')
80+
// .replace('"phone"', 'planter.phone as "phone"');
7681

7782
let selectStmt;
7883
if (deviceIdentifier) {
7984
selectStmt = `SELECT planter.* FROM planter ${this.getPlanterRegistrationJoinClause(
8085
deviceIdentifier,
8186
)}`;
8287
} else {
83-
selectStmt = `SELECT ${columnNames} FROM planter`;
88+
selectStmt = `SELECT planter.* FROM planter`;
8489
}
8590

8691
const params = {
@@ -90,7 +95,6 @@ export class PlanterRepository extends UtilsRepositoryMixin<
9095
};
9196

9297
const query = buildFilterQuery(selectStmt, params);
93-
// console.log('query ---------', query);
9498

9599
const result = await this.execute(query.sql, query.params, options);
96100
return <Planter[]>result.map((planter) => utils.convertCamel(planter));

0 commit comments

Comments
 (0)