Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
elios264 opened this issue Feb 2, 2018 · 3 comments
Assignees

Comments

@elios264
Copy link

elios264 commented Feb 2, 2018

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.

@dplewis
Copy link
Member

dplewis commented Feb 8, 2018

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?

dplewis added a commit to dplewis/parse-server that referenced this issue Feb 8, 2018
@elios264
Copy link
Author

elios264 commented Feb 8, 2018

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!

@dplewis
Copy link
Member

dplewis commented Feb 8, 2018

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants