-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Reusing schemas end up with no index creation #13054
Comments
When I was reusing the schemaProperties somewhere else, I was there stating unique: false. As I am quite new to JS, I didnt pay attention that the variable seemed to be passed by reference which apply the modification to the original object. So, for one side I am happy that I fixed the issue, but I would like to still mantain the questions related to "how to reuse schemas and schema properties". Edit:
The issue with this code that made me look for alternatives is that the legalId is defined as unique in the investorSchema, then I got duplicate issue when storing transfers with same investors. |
Just in case this might help someone else in the future, I could fix the issue by using clone(), as follows:
|
We'll keep this open for now, this may be a bug |
if you uncomment the lines, it will throw an error. Otherwise, it will behave like nothing is wrong. const mongoose = require('mongoose');
const investorLegalIdProperty = {
type: String,
required: true,
unique: true,
minlength: 5,
maxlenght: 50,
};
const investorSchema = new mongoose.Schema({
name: {
type: String,
required: true,
minlength: 2,
maxlenght: 50,
},
familyName: {
type: String,
required: true,
minlength: 2,
maxlenght: 50,
},
legalId: investorLegalIdProperty,
});
const testSchema = new mongoose.Schema({
age: Number,
legalId: investorLegalIdProperty
});
const Test = mongoose.model('Test', testSchema);
const Investor = mongoose.model('Investor', investorSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
// await Test.init();
// const indexes = await Test.listIndexes();
// console.log(indexes);
// await Investor.init();
// const index = await Investor.listIndexes();
// console.log(index);
await Test.create({
age: 1,
legalId: 'THEID'
});
await Investor.create({
name: 'Test',
familyName: 'Testserson',
legalId: 'THEID'
});
await Test.create({
age: 1,
legalId: 'THEID'
});
await Investor.create({
name: 'Test',
familyName: 'Testserson',
legalId: 'THEID'
});
console.log('done');
}
run(); |
Your script is not throwing an error if you leave the line un-commented because the If we run:
We won't get any errors. |
@lpizzinidev is right, @IslandRhythms 's repro script is incorrect. If you don't wait for indexes to finish building, then you won't get unique constraint. |
Prerequisites
Mongoose version
6.8.1
Node.js version
16.18.0
MongoDB version
5.0.14
Operating system
Linux
Operating system version (i.e. 20.04, 11.3, 10)
No response
Issue
I am a new user of mongoose library, and I have the following topic.
I have the need to reuse properties of schemas (I assume this is a pretty common need, please correct me if my assumption is wrong), e.g. :
Then I have schemas that use this property as follows:
This approach was (in my naive mind) very convenient way to reuse the investorLegalIdProperty in different schemas, and has been working fine for the [type], the [minlenght] and the [maxlenght]. However that is not the case for [required] and [unique], which are completely ignored and indexes are not created accordingly.
My questions are:
Thanks for any support.
The text was updated successfully, but these errors were encountered: