2015-10-03 30 views
0

我有一個名爲Xml_Mapping的表,使用MYSQL上的組合ID。 當我嘗試插入表;在運行時或從StrongLoop API資源管理器中,我無法插入。插入模式在迴環中使用複合ID的模型不起作用

當我調試,我看到錯誤插入SQL:

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

,你可以看到, 「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

改變你的表Xml_Mapping,並設置自動增量領域SITEID ,你不需要在JSON中提供SiteId

運行

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

更改模型XML的mapping.json:並設置屬性SITEID「需要」:假的, 應該是這樣

{ 
    "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' 
}