Belong to relationship doest connected to the hasMany table (Mongodb)

Hey, I’ve created this mongoose model (companies, hasMany branches):

  const schema = Mongoose.Schema({
    name: {
      type: String,
      required: true,
    },
    description: {
      type: String,
      required: false,
    },
    logo: {
      type: String,
      required: false,
    },
    branches: [{ type: Mongoose.Schema.Types.ObjectId, ref: 'branches' }],
    carCategories: [{ type: Mongoose.Schema.Types.ObjectId, ref: 'carCategories' }],
  }, {
    timestamps: true,
  });

Now, I’ve created another model called branches (belong to company)

const schema = Mongoose.Schema({
    name: {
      type: String,
      required: true,
    },
    address: {
      type: address(mongoose, Mongoose),
      required: false,
    },
    companyId: { type: Mongoose.Schema.Types.ObjectId, ref: 'companies' },
  }, {
    timestamps: true,
  });

Expected behavior

On creating a new branch and connecting it to a company the company will contains the new branch.

Actual behavior

If I create the new branch from the branches collection the branch is connected to the company but the company doesn’t contain the branch.
If I create the branch directly from the company everything is working well.

Failure Logs

No logs (please let me know if a video will help

Context

“express”: “~4.17.1”,
“forest-express-mongoose”: “^8.4.1”,
“mongoose”: “^6.0.12”,

  • Project Name: clickvesa

Hey @clickvesa, and welcome to our community :wave:

In your case, and from the perspective of just creating a branch, this is the expected behavior.

The fact that you want a complete list of branches associated to a company is a specific behavior, so a bit of code will be required.

If you want to update the company when creating a branch (In order to have the complete list of branches associated to the company), you’ll need to override the POST /branches route in order to update the company as well.

Let me know if that helps :pray:

Thank you @jeffladiray for this replay, I’ll try to override this route. but I’m just curious why there is a difference between creating the branch from the branches collection to creating the same branch from a company, in both cases we’re associate this branch both ways (company includes branch and branch included company).

1 Like

Here is a bit of detail:

  • When creating a branch, you only create a branch - In the definition of your model, the branch only has a “BelongsTo” type of relationship, but no other link to the company.
  • When creating/updating a company, both models are updated/created. From a model perspective, you create a branch on a company, so the company is updated (HasMany), and the branch is created (BelongsTo company).

I’m not sure my explanation is clear enough, so let me know :slight_smile:

Got it, thank you, I’ll try the override solution.

I’m closing this for now then, don’t hesitate to open a new thread if you encounter issues on the override implementation :pray:

1 Like