Skip to content

Receiving a HTTP 500 when trying to save a duplicated value on an indexed unique field #4542

Closed

Description

@elios264

Issue Description

Parse-Server responds with a HTTP 500 when trying to save a duplicate value on an indexed unique column.

Steps to reproduce

1.- Setup a new parse-server instance and add a field of the type String to the table.
2.- Create a unique index on that field
3.- Try to save 2 records with the same value for the indexed field.

Expected Results

The server should return a Parse.Error.DUPLICATE_VALUE, error code

Actual Outcome

The server responds with a HTTP 500:

error: Error generating response. { MongoError: E11000 duplicate key error index:
error: Uncaught internal server error. { MongoError: E11000 duplicate key error index: 

Environment Setup

  • Server

    • parse-server version 2.7.2
    • Operating System: Windows & Linux
    • Hardware: DigitalOcean droplet, my computer,
    • Localhost or remote server? both
  • Database

    • MongoDB version: 3.4.11
    • Storage engine: wiredTiger
    • Hardware: 1 GB Memory / 25 GB Disk / SFO2
    • Localhost or remote server? DigitalOcean, localhost

Logs/Trace

error: Error generating response. { MongoError: E11000 duplicate key error index: ecommerce-universal.ProductCat.$name_1 dup key: { : "YAMATO" }
    at C:\Users\elios\elios' files\Projects\X-Evolvers\PRGNRS\ecommerce-universal\node_modules\mongodb-core\lib\connection\pool.js:595:61
    at authenticateStragglers (C:\Users\elios\elios' files\Projects\X-Evolvers\PRGNRS\ecommerce-universal\node_modules\mongodb-core\lib\connection\pool.js:513:16)
    at Connection.messageHandler (C:\Users\elios\elios' files\Projects\X-Evolvers\PRGNRS\ecommerce-universal\node_modules\mongodb-core\lib\connection\pool.js:549:5)
    at emitMessageHandler (C:\Users\elios\elios' files\Projects\X-Evolvers\PRGNRS\ecommerce-universal\node_modules\mongodb-core\lib\connection\connection.js:309:10)
    at Socket.<anonymous> (C:\Users\elios\elios' files\Projects\X-Evolvers\PRGNRS\ecommerce-universal\node_modules\mongodb-core\lib\connection\connection.js:452:17)
    at Socket.emit (events.js:159:13)
    at addChunk (_stream_readable.js:265:12)
    at readableAddChunk (_stream_readable.js:252:11)
    at Socket.Readable.push (_stream_readable.js:209:10)
    at TCP.onread (net.js:598:20)
  name: 'MongoError',
  message: 'E11000 duplicate key error index: ecommerce-universal.ProductCat.$name_1 dup key: { : "YAMATO" }',
  ok: 0,
  errmsg: 'E11000 duplicate key error index: ecommerce-universal.ProductCat.$name_1 dup key: { : "YAMATO" }',
  code: 11000,
  codeName: 'DuplicateKey' } name=MongoError, message=E11000 duplicate key error index: ecommerce-universal.ProductCat.$name_1 dup key: { : "YAMATO" }, ok=0, errmsg=E11000 duplicate key error index: ecommerce-universal.ProductCat.$name_1 dup key: { : "YAMATO" }, code=11000, codeName=DuplicateKey
error: Uncaught internal server error. { MongoError: E11000 duplicate key error index: ecommerce-universal.ProductCat.$name_1 dup key: { : "YAMATO" }
    at C:\Users\elios\elios' files\Projects\X-Evolvers\PRGNRS\ecommerce-universal\node_modules\mongodb-core\lib\connection\pool.js:595:61
    at authenticateStragglers (C:\Users\elios\elios' files\Projects\X-Evolvers\PRGNRS\ecommerce-universal\node_modules\mongodb-core\lib\connection\pool.js:513:16)
    at Connection.messageHandler (C:\Users\elios\elios' files\Projects\X-Evolvers\PRGNRS\ecommerce-universal\node_modules\mongodb-core\lib\connection\pool.js:549:5)
    at emitMessageHandler (C:\Users\elios\elios' files\Projects\X-Evolvers\PRGNRS\ecommerce-universal\node_modules\mongodb-core\lib\connection\connection.js:309:10)
    at Socket.<anonymous> (C:\Users\elios\elios' files\Projects\X-Evolvers\PRGNRS\ecommerce-universal\node_modules\mongodb-core\lib\connection\connection.js:452:17)
    at Socket.emit (events.js:159:13)
    at addChunk (_stream_readable.js:265:12)
    at readableAddChunk (_stream_readable.js:252:11)
    at Socket.Readable.push (_stream_readable.js:209:10)
    at TCP.onread (net.js:598:20)
  name: 'MongoError',
  message: 'E11000 duplicate key error index: ecommerce-universal.ProductCat.$name_1 dup key: { : "YAMATO" }',
  ok: 0,

This was working as expected before, I tried going back to older parse-server versions but even so, keeps happening.

Activity

  1. dplewis commented on Feb 8, 2018

    @dplewis
    Member

    Parse Server allows you to create indexes but not unique indexes.

    Can you post some sample code?
    Did you go though a Mongo Service like MLab or use Mongo Shell?

  2. added a commit that references this issue on Feb 8, 2018
  3. elios264 commented on Feb 8, 2018

    @elios264
    Author

    Hi @dplewis,
    I tried both, custom mongo installation, and Mlab, you are right, Parse don't allow you to create unique indexes, but I created them directly through Mlab or using the mongodb package,

    About the code sample:

    /* global Parse */
    const MongoClient = require('mongodb').MongoClient;
    
    //assuming master permissions.
    const poc = async () => {
    
      const productSchema = new Parse.Schema('Product');
      productSchema.addString('code');
      await productSchema.save();
    
      const client = await MongoClient.connect(process.env.PARSE_MONGO_STORAGE);
      const db = client.db(client.s.options.dbName);
    
      await db.collection(productSchema.className).createIndexes([
        { name: 'code_idx', unique: true, key: { 'code': 1 } },
      ]);
    
      const obj = new Parse.Object('Product');
      obj.set('code', '1');
      await obj.save();
    
      const obj2 = new Parse.Object('Product');
      obj2.set('code', '2');
      await obj2.save();
    
      obj.set('code', '2');
      await obj.save(); //parse responds with 500 error code here and logs exceptions and callstack to the console.
    };
    
    poc();
    

    I know creating unique indexes can not be done through the Parse Server api,
    but Parse does create unique indexes internally for username and email on the _User class, and when you try to save a duplicate Parse responds with a Parse.Error.DUPLICATE_VALUE,

    I know since this is not currently supported by Parse Server, this could not be considered as an issue,
    and I could create a beforeSave trigger and look for the value before proceeding, but I feel that the db indexes are the best and most efficient place to put this logic.

    Thank you for attending this issue!

  4. dplewis commented on Feb 8, 2018

    @dplewis
    Member

    @elios264 Thanks for the quick response. I don't see why we couldn't add unique indexes as a feature of Parse in the future. But for now I can put together a pull request for this.

  5. self-assigned this
    on Feb 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions