mongoose-sequence icon indicating copy to clipboard operation
mongoose-sequence copied to clipboard

using this with two id fields is throwing errors

Open ponypaw opened this issue 3 years ago • 2 comments

I've been trying to make this work with two schemas in my app and I cant seem to figure it out. I found a reference to someone else with an issue here and used their solution as a guide but its not working. I have 2 schemas set up, "userSchema" and "petSchema". Below each schema I have:

userSchema.plugin(AutoIncrement, {collection_name: 'counters'});

and

petSchema.plugin(AutoIncrement, {collection_name: 'petCounters'});

as a part of them both, the schemas have:

id: { type: Number, primaryKey: true },

But when I try to use the schemas, I get this error:

C:\Users\ponyp\Desktop\MiwV2\node_modules\mongoose-sequence\lib\sequence.js:170 throw new Error('Auto increment field already present and not of type "Number"'); ^

Error: Auto increment field already present and not of type "Number" at SequenceFactory.Sequence._createSchemaKeys (C:\Users\ponyp\Desktop\MiwV2\node_modules\mongoose-sequence\lib\sequence.js:170:13) at SequenceFactory.Sequence.enable (C:\Users\ponyp\Desktop\MiwV2\node_modules\mongoose-sequence\lib\sequence.js:112:10) at SequenceFactory.Sequence.getInstance (C:\Users\ponyp\Desktop\MiwV2\node_modules\mongoose-sequence\lib\sequence.js:95:14) at Schema.plugin (C:\Users\ponyp\Desktop\MiwV2\node_modules\mongoose\lib\schema.js:1430:3) at Object. (C:\Users\ponyp\Desktop\MiwV2\APP.JS:61:12) at Module._compile (internal/modules/cjs/loader.js:1063:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10) at Module.load (internal/modules/cjs/loader.js:928:32) at Function.Module._load (internal/modules/cjs/loader.js:769:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) at internal/main/run_main_module.js:17:47

Can you please help me figure out what's going wrong? I am still a beginner and I'm having a hard time figuring out what's up. I gather it's not giving the newly made pet an ID number but I can't tell why.

ponypaw avatar Apr 30 '21 20:04 ponypaw

Can you send the schemas (relevant part) and the plugin instantiation? Can you please try to better format the code (use backtick ` or the button < >).
Can you try to assign two different ids to the autoIncrement? Something like this

userSchema.plugin(AutoIncrement, {id: 'user_id_counter', inc_field: 'id' });
petSchema.plugin(AutoIncrement, {id: 'pet_id_counter', inc_field: 'id' });

ramiel avatar Apr 30 '21 23:04 ramiel

Also, is primaryKey: true part of mongoose :thinking: ? I think it's not, you have to define an index on the id field if you want to simulate the effect of a primary key.

ramiel avatar Apr 30 '21 23:04 ramiel