loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Model JSON default scope declaration breaks update

Open spirospolitis opened this issue 11 years ago • 25 comments

Assuming a model definition like so:

(test-model.json)

{
  "name": "TestModel",
  "plural": "TestModels",
  "base": "PersistedModel",
  "idInjection": false,
  "mysql": {
    "schema": "test_schema",
    "table": "test_model"
  },
  "properties": {
     "id": {
      "type": "Number",
      "id": true,
      "required": true,
      "index": true,
      "length": null,
      "precision": 11,
      "scale": 0,
      "mysql": {
        "columnName": "id",
        "dataType": "int",
        "dataLength": null,
        "dataPrecision": 11,
        "dataScale": 0,
        "nullable": "N"
      },
      "_selectable": true
    },
    "name": {
      "type": "String",
      "required": true,
      "length": 100,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "name",
        "dataType": "varchar",
        "dataLength": 100,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "N"
      },
      "_selectable": true
    },
    "deleted": {
        "type": "Boolean",
        "required": true,
        "length": null,
        "precision": null,
        "scale": null,
        "mysql": {
            "columnName": "deleted",
            "dataType": "tinyint",
            "dataLength": null,
            "dataPrecision": 1,
            "dataScale": 0,
            "nullable": "N"
        },
        "_selectable": false
    }
  },
  "scope": {
      "where": {
          "deleted": false
      }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": []
}

and having overridden the TestModel.deleteById method:

(test-model.js)

module.exports = function(TestModel) {
    TestModel.on('attached', function() {
        TestModel.deleteById = function(id, cb) {
            console.log('Overridden TestModel.deleteById');

            TestModel.update({'id': id}, {'deleted': true}, cb);
        }
    });
};

the query produced is the following (datasource "debug": true):

--> ComQueryPacket
{ command: 3,
  sql: 'UPDATE `test_model` SET `deleted` = 0 WHERE (`id` = 1) AND (`deleted` = 0)' }

Seems that the default scope column "deleted" value is assigned as the SET value of the "deleted" param.

Note that if one is to remove the default scope declaration from the .json file, the update works as expected.

Regards.

spirospolitis avatar Jan 16 '15 12:01 spirospolitis

dao.js screenshot 2017-04-10 14 26 39

utils.js screenshot 2017-04-10 14 25 32

// setScopeValuesFromWhere(data, scope.where, this); works

idushes avatar Apr 10 '17 19:04 idushes

Solution server.js before boot

screenshot 2017-04-10 15 53 58

idushes avatar Apr 10 '17 20:04 idushes

Can't loopback just make a feature to set a scope option to include or exclude specific calls

xardit avatar Dec 31 '20 16:12 xardit

LoopBack 3 has reached end of life, we are not going to add any new features.

bajtos avatar Jan 05 '21 14:01 bajtos

@bajtos Also LoopBack 4 has the same problem, the scope interferes everywhere on every crud operation According to the loopback overview logic it shouldn't loopback-overview

Anyway good luck, i extended to my models to another repository that extended to default crud repository, that way i could just remove the scope completely and override the field i needed on every crud operation method.

xardit avatar Jan 07 '21 13:01 xardit