ValidationError: Entity X does not have property Y

If you get the following error when retrieving data either from an API route, using Query, or using a module's service:

Terminal
ValidationError: Entity X does not have property Y

Why this Error Occurred#

There are different reasons why this error may occur. This troubleshooting guide will help you identify the cause of the error.

Incorrect Property Name#

The most common reason for this error is that the property name you're using is incorrect. Make sure the property name is spelled correctly and matches the property name in the entity.

For example, a common mistake is retrieving variant.option instead of variant.options.

If you're retrieving a property from a Commerce Module, check out the data model references in the Commerce Modules documentation to ensure you're using the correct property name.

Referencing Linked Model with Module's Service#

Another common mistake is passing a linked data model's name to a module's service, which isn't allowed.

For example, retrieving the customer of an order using the Order Module's service is not allowed:

Code
1// Don't do this2const { data } = await orderService.retrieveOrder("order_123", {3  relations: ["customer"],4})

This isn't allowed because the Order Module's service retrieves data only within the module's scope.

To retrieve a linked data model like customer, use Query instead:

Code
1// use req.scope instead of container in API routes2const query = container.resolve("query")3
4const { data } = await query.graph({5  entity: "order",6  fields: ["customer.*"],7  filters: {8    id: "order_123",9  },10})

Referencing Non-Existent Relation#

To expand a relation of a data model, the data model must have a relationship property in its definition.

For example, consider you have the following data model:

Code
1import { model } from "@medusajs/framework/utils"2
3const Post = model.define("post", {4  id: model.id().primaryKey(),5  title: model.text(),6  author_id: model.text(),7})8
9export default Post

Even if you have an Author data model, it's not possible to retrieve the post's author using the author:

Code
1// Don't do this2const post = await postService.retrievePost("post_123", {3  relations: ["author"],4})

This will throw the ValidationError because the Post data model doesn't have a relationship property with the Author data model.

Instead, add the author as a relationship property to the Post data model:

Code
1import { model } from "@medusajs/framework/utils"2import Author from "./author"3
4const Post = model.define("post", {5  id: model.id().primaryKey(),6  title: model.text(),7  author: model.belongsTo(() => Author, {8    mappedBy: "posts",9  }),10})11
12export default Post

Now you can retrieve the post's author using the author:

Code
1// You can do this now2const post = await postService.retrievePost("post_123", {3  relations: ["author"],4})

Additional Resources#

Was this page helpful?
Ask Anything
FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break