CloudKit Query on Custom Indexed Field fails with misleading "createdBy is not queryable" error

Hello everyone,

I am experiencing a persistent authentication error when querying a custom user profile record, and the error message seems to be a red herring.

My Setup:

I have a custom CKRecord type called ColaboradorProfile.

When a new user signs up, I create this record and store their hashed password, salt, nickname, and a custom field called loginIdentifier (which is just their lowercase username).

In the CloudKit Dashboard, I have manually added an index for loginIdentifier and set it to Queryable and Searchable. I have deployed this schema to Production.

The Problem: During login, I run an async function to find the user's profile using this indexed loginIdentifier.

Here is the relevant authentication code:

func autenticar() async {
    // ... setup code (isLoading, etc.)
    let lowercasedUsername = username.lowercased()

    // My predicate ONLY filters on 'loginIdentifier'
    let predicate = NSPredicate(format: "loginIdentifier == %@", lowercasedUsername)
    let query = CKQuery(recordType: "ColaboradorProfile", predicate: predicate)
    
    // I only need these specific keys
    let desiredKeys = ["password", "passwordSalt", "nickname", "isAdmin", "isSubAdmin", "username"]
    let database = CKContainer.default().publicCloudDatabase

    do {
        // This is the line that throws the error
        let result = try await database.records(matching: query, desiredKeys: desiredKeys, resultsLimit: 1)
        
        // ... (rest of the password verification logic)

    } catch {
        // The error always lands here
        logDebug("Error authenticating with CloudKit: \(error.localizedDescription)")
        await MainActor.run {
            self.errorMessage = "Connection Error: \(error.localizedDescription)"
            self.isLoading = false
            self.showAlert = true
        }
    }
}

The Error: Even though my query predicate only references loginIdentifier, the catch block consistently reports this error:

Error authenticating with CloudKit: Field 'createdBy' is not marked queryable.

I know createdBy (the system creatorUserRecordID) is not queryable by default, but my query isn't touching that field. I already tried indexing createdBy just in case, but the error persists. It seems CloudKit cannot find or use my index for loginIdentifier and is incorrectly reporting a fallback error related to a system field.

Has anyone seen this behavior? Why would CloudKit report an error about createdBy when the query is explicitly on an indexed, custom field?

I'm new to Swift and I'm struggling quite a bit.

Thank you,

CloudKit Query on Custom Indexed Field fails with misleading "createdBy is not queryable" error
 
 
Q