Skip to content

Commit fedeff9

Browse files
committed
Merge pull request #864 from Marco129/fix-delete-field
Fix delete relation field when _Join collection not exist
2 parents 7e153d4 + 908a4eb commit fedeff9

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

spec/Schema.spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,38 @@ describe('Schema', () => {
577577
});
578578
});
579579

580+
it('can delete relation field when related _Join collection not exist', done => {
581+
config.database.loadSchema()
582+
.then(schema => {
583+
schema.addClassIfNotExists('NewClass', {
584+
relationField: {type: 'Relation', targetClass: '_User'}
585+
})
586+
.then(mongoObj => {
587+
expect(mongoObj).toEqual({
588+
_id: 'NewClass',
589+
objectId: 'string',
590+
updatedAt: 'string',
591+
createdAt: 'string',
592+
relationField: 'relation<_User>',
593+
});
594+
})
595+
.then(() => config.database.collectionExists('_Join:relationField:NewClass'))
596+
.then(exist => {
597+
expect(exist).toEqual(false);
598+
})
599+
.then(() => schema.deleteField('relationField', 'NewClass', config.database))
600+
.then(() => schema.reloadData())
601+
.then(() => {
602+
expect(schema['data']['NewClass']).toEqual({
603+
objectId: 'string',
604+
updatedAt: 'string',
605+
createdAt: 'string'
606+
});
607+
done();
608+
});
609+
});
610+
});
611+
580612
it('can delete string fields and resave as number field', done => {
581613
Parse.Object.disableSingleInstance();
582614
var obj1 = hasAllPODobject();

src/Schema.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,11 @@ class Schema {
409409

410410
if (this.data[className][fieldName].startsWith('relation<')) {
411411
//For relations, drop the _Join table
412-
return database.dropCollection(`_Join:${fieldName}:${className}`);
412+
return database.collectionExists(`_Join:${fieldName}:${className}`).then(exist => {
413+
if (exist) {
414+
return database.dropCollection(`_Join:${fieldName}:${className}`);
415+
}
416+
});
413417
}
414418

415419
// for non-relations, remove all the data.

0 commit comments

Comments
 (0)