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

Fix regression for overwriteModels flag #12359

Merged
merged 3 commits into from
Aug 30, 2022
Merged

Conversation

chochihim
Copy link
Contributor

Below code throws MissingSchemaError in the populate statement:

const mongoose = require("mongoose");

const { Schema } = mongoose;

async function run() {
  try {
    await mongoose.connect(process.env.MONGODB_URI);

    mongoose.set("overwriteModels", true);

    const personSchema = Schema({
      name: String,
    });

    const storySchema = Schema({
      author: { type: Schema.Types.ObjectId, ref: "Person" },
      title: String,
    });

    const Story = mongoose.model("Story", storySchema);
    const Person = mongoose.model("Person", personSchema);

    const person = await Person.create({ name: "David" });
    await Story.create({
      title: "My Story",
      author: person._id,
    });

    await Story.findOne({ title: "My Story" })
      .populate("author")
      .exec();
  } catch (error) {
    console.error(error);
  }
}

run();

It will works if I do not set overwriteModels as true.

I find that it is caused by 6865fea. Looks like that commit prevents mixing models registered in non-default connection and global mongoose instance.

How about adding back the line for model lookup but lookup from the connection itself (lookup from this.models instead of this.base.models)?

Summary

Examples

Below code throws `MissingSchemaError` in the `populate` statement:
```
const mongoose = require("mongoose");

const { Schema } = mongoose;

async function run() {
  try {
    await mongoose.connect(process.env.MONGODB_URI);

    mongoose.set("overwriteModels", true);

    const personSchema = Schema({
      name: String,
    });

    const storySchema = Schema({
      author: { type: Schema.Types.ObjectId, ref: "Person" },
      title: String,
    });

    const Story = mongoose.model("Story", storySchema);
    const Person = mongoose.model("Person", personSchema);

    const person = await Person.create({ name: "David" });
    await Story.create({
      title: "My Story",
      author: person._id,
    });

    await Story.findOne({ title: "My Story" })
      .populate("author")
      .exec();
  } catch (error) {
    console.error(error);
  }
}

run();
```
It will works if I do not set `overwriteModels` as `true`.

I find that it is caused by 6865fea. Looks like that commit prevents mixing models registered in non-default connection and global mongoose instance. 

How about adding back the line for model lookup but lookup from the connection itself (lookup from `this.models` instead of `this.base.models`)?
Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works, but I think the more correct fix would be to just return this.models[name] if calling this method with 1 argument. What do you think?

lib/connection.js Outdated Show resolved Hide resolved
@vkarpov15 vkarpov15 added this to the 6.5.4 milestone Aug 30, 2022
@vkarpov15 vkarpov15 merged commit f858ca5 into Automattic:master Aug 30, 2022
vkarpov15 added a commit that referenced this pull request Aug 30, 2022
vkarpov15 added a commit that referenced this pull request Aug 30, 2022
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

Successfully merging this pull request may close these issues.

None yet

2 participants