插入不适用于环回

时间:2015-10-03 21:59:19

标签: loopbackjs

我在MYSQL上有一个名为Xml_Mapping的表,其中包含复合ID。 当我尝试插入表格;在运行时或从StrongLoop API Explorer中,我无法插入。

当我调试时,我看到错误的插入sql:

    INSERT INTO `Xml_Mapping`(`SiteId`,`SystemKeyId`,`TargetKey`) VALUES(1,1,'stockcode') 
    ON DUPLICATE KEY UPDATE 

正如您所见,"更新" sql没有完成。所以它无法执行......

我该如何解决这个问题?

Xml_Mapping表:

    SiteId INT NOT NULL,
    SystemKeyId INT NOT NULL,
    TargetKey VARCHAR(100) NOT NULL,
    PRIMARY KEY (SiteId, SystemKeyId, TargetKey),
    CONSTRAINT XML_MAPPING_fk1 FOREIGN KEY (SiteId) REFERENCES site (Id),
    INDEX XML_MAPPING_fk1 (SiteId)

Xml-mapping.json:

{
  "name": "XmlMapping",
  "base": "PersistedModel",
  "idInjection": false,
  "options": {
    "validateUpsert": true
  },
  "mysql": {
    "schema": "uygunca",
    "table": "Xml_Mapping"
  },
  "properties": {
    "SiteId": {
      "type": "Number",
      "id": 1,
      "required": true,
      "length": null,
      "precision": 10,
      "scale": 0,
      "mysql": {
        "columnName": "SiteId",
        "dataType": "int",
        "dataLength": null,
        "dataPrecision": 10,
        "dataScale": 0,
        "nullable": "N"
      },
      "_selectable": false
    },
    "SystemKeyId": {
      "type": "Number",
      "id": 2,
      "required": true,
      "length": null,
      "precision": 10,
      "scale": 0,
      "mysql": {
        "columnName": "SystemKeyId",
        "dataType": "int",
        "dataLength": null,
        "dataPrecision": 10,
        "dataScale": 0,
        "nullable": "N"
      },
      "_selectable": false
    },
    "TargetKey": {
      "type": "String",
      "id": 3,
      "required": true,
      "length": 100,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "TargetKey",
        "dataType": "varchar",
        "dataLength": 100,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "N"
      },
      "_selectable": false
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": []
}

1 个答案:

答案 0 :(得分:1)

更改表 Xml_Mapping 并为字段 SiteId 设置自动增量 并且您不需要在JSON中提供 SiteId

运行

ALTER TABLE `Xml_Mapping`
MODIFY `SiteId` int(10) NOT NULL AUTO_INCREMENT

更改您的模型 Xml-mapping.json:并设置 SiteId "必需":false, 它应该像

{
  "name": "XmlMapping",
  "base": "PersistedModel",
  "idInjection": false,
  "options": {
    "validateUpsert": true
  },
  "mysql": {
    "schema": "uygunca",
    "table": "Xml_Mapping"
  },
  "properties": {
    "SiteId": {
      "type": "Number",
      "id": 1,
      "required": false,
      "length": null,
      "precision": 10,
      "scale": 0,
      "mysql": {
        "columnName": "SiteId",
        "dataType": "int",
        "dataLength": null,
        "dataPrecision": 10,
        "dataScale": 0,
        "nullable": "N"
      },
      "_selectable": false
    },
    "SystemKeyId": {
      "type": "Number",
      "id": 2,
      "required": true,
      "length": null,
      "precision": 10,
      "scale": 0,
      "mysql": {
        "columnName": "SystemKeyId",
        "dataType": "int",
        "dataLength": null,
        "dataPrecision": 10,
        "dataScale": 0,
        "nullable": "N"
      },
      "_selectable": false
    },
    "TargetKey": {
      "type": "String",
      "id": 3,
      "required": true,
      "length": 100,
      "precision": null,
      "scale": null,
      "mysql": {
        "columnName": "TargetKey",
        "dataType": "varchar",
        "dataLength": 100,
        "dataPrecision": null,
        "dataScale": null,
        "nullable": "N"
      },
      "_selectable": false
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": []
}

然后将您的JSON用作

{
  "SystemKeyId": 1,
  "TargetKey": 'stockcode'  
}