Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    solution for changing the primaryKey

    The SmartClient documentation and http://forums.smartclient.com/showthread.php?t=6333, comment #4 says: "If you're changing the primaryKey, do a remove then add."

    But I found a trivial solution which works (at least for me) without problems.

    The change is in file ResultSet.js, method updateCacheData():
    Code:
    ...
              // if we didn't find the record in our cache, check for the case where the server
              // returned a changed (bad) primary key value for an update of an existing record.
              // In this case warn, and clear out the original version of the record.
              if (index == -1) {
                  var submittedRecord = dsRequest.data;
                  if (isc.isAn.Array(submittedRecord)) submittedRecord = submittedRecord[0];
      
                  // pare down to PKs and find in our data-set
                      submittedRecord = isc.applyMask(submittedRecord, keyColumns);
                  var oldRecordIndex = this.getDataSource().findByKeys(submittedRecord, cache); 
                  if (oldRecordIndex != -1) {
                      this.logWarn("Update operation - submitted record with primary key value[s]:" + 
                                  this.echo(submittedRecord) + " returned with modified primary key:" + 
                                  this.echo(keyValues) + ". This may indicate bad server logic. " + "Updating cache to reflect new primary key.");
                      // remove the old record from our dataSet. If it matches filter, we'll re-add below
                      removedRows++;
                      cache.removeAt(oldRecordIndex);
                      // this is a weird case - don't attempt to fire row-specific dataChanged
                      delete this._lastUpdateData;
                  }
                  // <==== changes to handle modification of the primary key ====
                  else {
                    submittedRecord = dsRequest.oldValues;
                    if (submittedRecord) {
                      submittedRecord = isc.applyMask(submittedRecord, keyColumns);
                      var oldRecordIndex = this.getDataSource().findByKeys(submittedRecord, cache); 
                      if (oldRecordIndex != -1) {
                        this.logWarn("Update operation - submitted record with old primary key value[s]:" + 
                                    this.echo(submittedRecord) + " returned with new primary key:" + 
                                    this.echo(keyValues) + "Updating cache to reflect new primary key.");
                        // remove the old record from our dataSet. If it matches filter, we'll re-add below
                        removedRows++;
                        cache.removeAt(oldRecordIndex);
                        // this is a weird case - don't attempt to fire row-specific dataChanged
                        delete this._lastUpdateData;
                      }
                    }
                  }
                  // ==== end of changes to handle modification of the primary key ====>
              } else if (updateData.length == 1) {
                  origRecord = cache.get(index);
                  // catch the case where the orig record is present in the allRows cache but doesn't
                  // match filter criteria for local data
                  if (filteringOnClient && 
    ...
    I also attached the complete source code of updateCacheData() method.

    Borut
    Attached Files

    #2
    This appears to be incorrect, note that further down in the method there is code to incorporate a row with a changed primary key into the cache:

    // - an "update" operation was submitted based on a record we have cached, but there's
    // a server bug where the server return the wrong PK, so we couldn't locate the
    // row. In this second case we've already removed the original record, so it's
    // appropriate to re-add the "new" record to the cache.
    Your attempted change appears to have the effect of allowing a row into the cache even if it doesn't match the current filter criteria, but that's incorrect.

    Comment

    Working...
    X