DefinedSchema: Indexes not created correctly for Pointer types
New Issue Checklist
/cc @Moumouls
- [x] I am not disclosing a vulnerability.
- [x] I am not just asking a question.
- [x] I have searched through existing issues.
- [x] I can reproduce the issue with the latest version of Parse Server.
Issue Description
When creating indexes on Pointer fields, the index is not created on the correct field name in MongoDB
Parse will add a _p_ prefix to the field name, but DefinedSchema won't. This have for consequence to leave the Pointer field without an index, and creating an index on a non-existant field.
Had a chat with @Moumouls about this, here is his comment:
The correct version indeed is the version with the
_p_, it is a quick fix that was pushed during the pr of the defined schemas. I remember it needed some refactor to allow just the field name without the p because of validators.
Steps to reproduce
For example:
export default SchemaMigrations.makeSchema('Message', {
fields: {
recipient: { type: 'Pointer', targetClass: '_User', required: true },
},
indexes: {
recipient: { recipient: 1 },
},
Looking at the indexes:
db.getCollection('Message').stats()
{
"_id_": new NumberInt("749568"),
"recipient": new NumberInt("90112"),
}
In this case, Parse will create a DB field named _p_recipient, not recipient.
This have for effect of leaving the _p_recipient un-indexed, and creating an unused index recipient
Actual Outcome
Index is created on unused field recipient
Expected Outcome
Index need to be created on _p_recipient
Environment
Server
- Parse Server version:
5 - Operating system:
w11 - Local or remote host (AWS, Azure, Google Cloud, Heroku, Digital Ocean, etc):
gcloud
Database
- System (MongoDB or Postgres):
MongoDB - Database version:
4.4 - Local or remote host (MongoDB Atlas, mLab, AWS, Azure, Google Cloud, etc):
gcloud
Client
- SDK (iOS, Android, JavaScript, PHP, Unity, etc):
javascript - SDK version:
4
Logs
Thanks for opening this issue!
- 🚀 You can help us to fix this issue faster by opening a pull request with a failing test. See our Contribution Guide for how to make a pull request, or read our New Contributor's Guide if this is your first time contributing.
I can work on this, but in order to advance, I need additional details about the problem. Any advice or suggestions for me?
const server = await reconfigureServer();
const indexes = { complex: { createdAt: 1, updatedAt: 1, recipient:1 } };
const schemas = {
definitions: [{ className: 'Test', fields: { recipient: { type: 'Pointer', targetClass:'_User',require:true } }, indexes }],
};
await new DefinedSchemas(schemas, server.config).execute();
const schema = await new Parse.Schema('Test').get();
cleanUpIndexes(schema);
expect(schema.indexes).withContext("Schema indexes did not match expected indexes");
})
I ran this test case for this state but couldn't find a bug. Are you certain about this?
. ✓ should create index with p prefix for pointer
@sadortun
@rgunindi
Can you check :
-
schema.indexescontains_p_recipient -
MongoDB console
db.getCollection('Test').stats()
{
"_id_": new NumberInt("1234"),
"_p_recipient": new NumberInt("1234"),
}
@sadortun

definitions: [{
className: 'Test', fields: {
recipient: { type: 'Pointer', targetClass: '_User', required: true },
},
indexes: {
recipient: { recipient: 1 },
}
}
],
};
I re-run and the result same

where is the _p_recipient ?
@rgunindi
If you add data, you'll see in MongoDB the pointer field will be in the _p_recipient field. But the index is created on recipient.
So, the actual pointer will not be indexed.
Hi @sadortun @rgunindi , @sadortun is right using a "classic" index on Pointers in a Parse Schema will not work.
I added a quick fix when i worked on Defined Schemas, it's documented in the index section of the Defined Schema docs see: https://docs.parseplatform.org/defined-schema/guide/#indexes
I don't remember exactly but it was a quick fix and the PR to correctly support indexes on pointers without "p" was a little bit heavier than supporting the "p"
But feel free to open a PR to correctly support the pointer index, i'll be happy to review it !
@Moumouls @sadortun
That's the way it should be, isn't it?
Yes @rgunindi if you have a Parse Pointer on the recipient field !
@Moumouls I have applied the fix for this issue and opened pr. Thank you for your patience. Please report deficiencies
@mtrezza i think this could be closed, a fix is documented about using indexes on pointers, it's not the best DX, but it works fine and is documented: https://docs.parseplatform.org/defined-schema/guide/#indexes
What about the PR that has been submitted?