project-imas / encrypted-core-data

v2.0 - iOS Core Data encrypted SQLite store using SQLCipher
Other
785 stars 236 forks source link

Prefix columns with table in SELECT clause #299

Open jeffdgr8 opened 6 years ago

jeffdgr8 commented 6 years ago

When NSDictionaryResultType is used on a fetch request and a selected column is present on a joined table, an "ambiguous column name: columnName" error results. The columns should be prefixed with tableName.columnName to avoid this ambiguity.

I experienced this error EncryptedStoreErrorMessage = "ambiguous column name: serverId" with a fetch request on User with the predicate unit.domainLeft >= 11 AND unit.domainRight <= 16, where User.unit -> OrgUnit and both User and OrgUnit have serverId columns.

The SQL query generated by ECD:

SELECT DISTINCT serverId, localId FROM ecdUser
LEFT OUTER JOIN ecdOrgUnit AS [unit] ON ecdUser.unit__objectid = [unit].__objectID 
WHERE ([unit].domainLeft >= ? AND [unit].domainRight <= ?);

And the SQL query generated by the default NSSQLiteStoreType:

SELECT t0.ZSERVERID, t0.ZLOCALID FROM ZUSER t0
JOIN ZORGUNIT t1 ON t0.ZUNIT = t1.Z_PK
WHERE ( t1.ZDOMAINLEFT >= ? AND  t1.ZDOMAINRIGHT <= ?) 

With these changes, the ECD query is now:

SELECT DISTINCT ecdUser.serverId, ecdUser.localId FROM ecdUser 
LEFT OUTER JOIN ecdOrgUnit AS [unit] ON ecdUser.unit__objectid = [unit].__objectID  
WHERE ([unit].domainLeft >= ? AND [unit].domainRight <= ?);