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

Error Starting Server Due to mappedBy reference in module models #9569

Closed
vholik opened this issue Oct 14, 2024 · 1 comment
Closed

Error Starting Server Due to mappedBy reference in module models #9569

vholik opened this issue Oct 14, 2024 · 1 comment

Comments

@vholik
Copy link

vholik commented Oct 14, 2024

Bug report

Error starting server
Error: Loaders for module VehicleModuleService failed: VehicleYear.models has unknown 'mappedBy' reference: VehicleModel.vehicle_year

Describe the bug

I encountered an error when starting the server, related to the relationships between my models in one vehicle module. The error specifically points to an unknown mappedBy reference between VehicleYear and VehicleModel.

brand.ts

import { model } from "@medusajs/framework/utils";
import { VehicleYear } from "./year";

export const Brand = model.define("brand", {
  id: model
    .id({
      prefix: "bra",
    })
    .primaryKey(),
  name: model.text(),
  logo: model.text().nullable(),
  years: model.hasMany(() => VehicleYear),
});

year.ts

import { model } from "@medusajs/framework/utils";
import { Brand } from "./brand";
import { VehicleModel } from "./model";

export const VehicleYear = model.define("vehicle_year", {
  id: model
    .id({
      prefix: "year",
    })
    .primaryKey(),
  year: model.number(),
  brand: model.belongsTo(() => Brand, {
    mappedBy: "years",
  }),
  models: model.hasMany(() => VehicleModel),
});

model.ts

import { model } from "@medusajs/framework/utils";
import { VehicleYear } from "./year";
import { VehicleVariant } from "./variant";

export const VehicleModel = model.define("vehicle_model", {
  id: model
    .id({
      prefix: "model",
    })
    .primaryKey(),
  name: model.text(),
  years: model.belongsTo(() => VehicleYear, {
    mappedBy: "models",
  }),
});

I suspect the error might be due to having both model.belongsTo and model.hasMany in the same models (VehicleYear and VehicleModel).

System information

Medusa version (including plugins):
Node.js version:
Database:
Operating system:
Browser (if relevant):

Steps to reproduce the behavior

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior

A clear and concise description of what you expected to happen

Screenshots

If applicable, add screenshots to help explain your problem

Code snippets

If applicable, add code samples to help explain your problem

Additional context

Add any other context about the problem here

@thetutlage
Copy link
Contributor

thetutlage commented Oct 18, 2024

Hello @vholik

Sorry for the late reply. Actually, the error you get is because the DML is not able to discover the inverse of the relationship using the default naming convention.

So there are two ways to fix it.

  • First, we either stick to be conventional naming. In that case, the property name on the VehicleModel should be vehicle_year and not year.

    - years: model.belongsTo(() => VehicleYear, {
    + vehicle_year: model.belongsTo(() => VehicleYear, {
      mappedBy: "models",
    }),
  • Or we define a custom mappedBy property when defining the relationship on the VehicleYear model.

    models: model.hasMany(() => VehicleModel, {
    +  mappedBy: "years"
    }),

@linear linear bot closed this as completed Oct 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants