From 908a4eb6436e5b616c11cce43a605f30cf2c0b28 Mon Sep 17 00:00:00 2001 From: Marco129 Date: Sun, 6 Mar 2016 17:22:36 +0800 Subject: [PATCH] Fix delete relation field when _Join collection not exist --- spec/Schema.spec.js | 32 ++++++++++++++++++++++++++++++++ src/Schema.js | 6 +++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/spec/Schema.spec.js b/spec/Schema.spec.js index 62ce711ef6..6ab08d6abf 100644 --- a/spec/Schema.spec.js +++ b/spec/Schema.spec.js @@ -577,6 +577,38 @@ describe('Schema', () => { }); }); + it('can delete relation field when related _Join collection not exist', done => { + config.database.loadSchema() + .then(schema => { + schema.addClassIfNotExists('NewClass', { + relationField: {type: 'Relation', targetClass: '_User'} + }) + .then(mongoObj => { + expect(mongoObj).toEqual({ + _id: 'NewClass', + objectId: 'string', + updatedAt: 'string', + createdAt: 'string', + relationField: 'relation<_User>', + }); + }) + .then(() => config.database.collectionExists('_Join:relationField:NewClass')) + .then(exist => { + expect(exist).toEqual(false); + }) + .then(() => schema.deleteField('relationField', 'NewClass', config.database)) + .then(() => schema.reloadData()) + .then(() => { + expect(schema['data']['NewClass']).toEqual({ + objectId: 'string', + updatedAt: 'string', + createdAt: 'string' + }); + done(); + }); + }); + }); + it('can delete string fields and resave as number field', done => { Parse.Object.disableSingleInstance(); var obj1 = hasAllPODobject(); diff --git a/src/Schema.js b/src/Schema.js index 73eaa3254b..0ed5552792 100644 --- a/src/Schema.js +++ b/src/Schema.js @@ -409,7 +409,11 @@ class Schema { if (this.data[className][fieldName].startsWith('relation<')) { //For relations, drop the _Join table - return database.dropCollection(`_Join:${fieldName}:${className}`); + return database.collectionExists(`_Join:${fieldName}:${className}`).then(exist => { + if (exist) { + return database.dropCollection(`_Join:${fieldName}:${className}`); + } + }); } // for non-relations, remove all the data.