Is there any way I can update a schema to mark a field as required?

Hey guys, I’m trying to find out if there is any way I can update an existing schema to mark some fields as required.

Thanks in advance

You can add something like this in serverStartComplete.

async serverStartComplete() {
  const options = { required: true, defaultValue: 'hello world' };
  const schema = new Parse.Schema('ClassName')
  schema.addString('field', options);
  await schema.update();
}

Hey @dblythy, thanks for the reply. It would work if I didn’t have a schema already set with a field called “field”.

My problem is that a already have many schemas and I want to update the fields to set them as required.

Do you know any solution for that?

const schema = new Parse.Schema('ClassName')
schema.deleteField('field');
await schema.update();
// now field won't exist in schema, add back in.
schema.addString('field', options);
await schema.update();

@dblythy that works, but since the field will be removed there will be data loss. I was looking for a solution with no data loss.

Ah, okay. As far as I can tell, it’s not possible. Maybe you could open an issue in the Parse Server repo?

Alternatively, you could update the _SCHEMA collection manually. I’m not sure if this will have complications.

Add this to “_metadata”:

 "fields_options" : {
  "field" : {
    "required" : true,
    "defaultValue" : "hello world"
  }
}
1 Like

@dblythy that woks with no data loss. It’s gonna be a lit be tedious to do that for many fields in 73 classes but it’s a solution hahah. Thank you so much for helping me.