diff --git a/lib/connection.js b/lib/connection.js index 89629c7..6d44278 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -231,6 +231,7 @@ module.exports = (function () { //TODO: refactor: move this to own method!!! // Create OrientDB schema if (collection.attributes){ + log.debug('Creating DB class [' + tableName + '] for collection [' + collection.identity + ']'); var attributeName; for(attributeName in collection.attributes){ var linkedClass = null, @@ -241,8 +242,9 @@ module.exports = (function () { else if (typeof collection.attributes[attributeName] === 'function') continue; else if (collection.attributes[attributeName].model || collection.attributes[attributeName].references){ - attributeType = 'Link'; linkedClass = collection.attributes[attributeName].model || collection.attributes[attributeName].references; + var useLink = me.collectionsByIdentity[linkedClass].primaryKey === 'id'; + attributeType = useLink ? 'Link' : collection.pkFormat; } else if (collection.attributes[attributeName].foreignKey){ attributeType = 'Link'; @@ -264,12 +266,14 @@ module.exports = (function () { if(collection.attributes[attributeName].columnName) columnName = collection.attributes[attributeName].columnName; + //log.debug('attributeType for ' + attributeName + ':', attributeType); + if(attributeType){ klass.property.create({ name: columnName, type: attributeType }); - if(linkedClass) + if(attributeType.toLowerCase().indexOf('link') === 0 && linkedClass) linksToBeCreated.push({ attributeName: columnName, klass: klass, @@ -505,7 +509,7 @@ module.exports = (function () { collectionInstance = this.collections[collection]; attributes = collectionInstance.attributes; - _document = new Document(options, attributes); + _document = new Document(options, attributes, self); if(!_document.nestedAssociations) return self.dbCreate(collection, _document.values, cb); @@ -576,7 +580,7 @@ module.exports = (function () { // Catch errors from building query and return to the callback try { _query = new Query(options, schema, self.config); - _document = new Document(values, attributes); + _document = new Document(values, attributes, self); where = _query.getWhereQuery(collection); } catch(e) { log.error('Failed to compose update SQL query.', e); diff --git a/lib/document.js b/lib/document.js index 7b2fcff..59cc013 100644 --- a/lib/document.js +++ b/lib/document.js @@ -19,7 +19,7 @@ var _ = require('lodash'), * @api private */ -var Document = module.exports = function Document(values, schema) { +var Document = module.exports = function Document(values, schema, connection) { // Keep track of the current document's values this.values = {}; @@ -29,6 +29,9 @@ var Document = module.exports = function Document(values, schema) { // Nested associations that require creating documents this.nestedAssociations = null; + + // Connection + this.connection = connection; // If values were passed in, use the setter if(values){ @@ -139,7 +142,7 @@ Document.prototype.serializeValues = function serializeValues(values) { return; - // If a foreignKey, check if value matches a orientDB id and if so turn it into an recordId + // If a foreignKey, check if value matches a orientDB id and if so turn it into a recordId if (foreignKey && utils.matchRecordId(values[key])) { values[key] = new RID(values[key]); } @@ -173,9 +176,16 @@ Document.prototype.serializeValues = function serializeValues(values) { delete values[key]; } } - else if(foreignKey || self.schema[schemaKey].model || self.schema[schemaKey].collection) { - // doesn't match a orientDB id, invalid fKey. Let's suppress it as Waterline does not expect an error - log.warn('Nullifying [' + key + '] as original value is undefined and Waterline does not expect an error.'); + else if(self.schema[schemaKey].model || self.schema[schemaKey].collection) { + var targetCollectionName = self.schema[schemaKey].model || self.schema[schemaKey].collection; + var targetCollection = self.connection.collectionsByIdentity[targetCollectionName]; + if(targetCollection.primaryKey === 'id'){ + log.warn('Nullifying foreign key [' + key + '] (value: [' + values[key] + ']) as value is not a RID and Waterline does not expect an error.'); + values[key] = null; + } + } + else if(foreignKey) { + log.warn('Nullifying foreign key [' + key + '] (value: [' + values[key] + ']) as value is not a RID and Waterline does not expect an error.'); values[key] = null; } diff --git a/package.json b/package.json index 1e1657b..afb3cf4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "waterline-orientdb", - "version": "0.10.17", + "version": "0.11.0", "description": "OrientDB adapter for Waterline / Sails.js ORM", "main": "./lib/adapter.js", "scripts": {