Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversHalloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk4 min

MedusaJS `defineLink`: What Cross-Module Links Mean for Foreign Keys

Medusa v2’s `defineLink` preserves module isolation by storing cross-module associations in a separate link table. Here’s what its missing foreign keys mean for integrity, lifecycle behavior, and deployment.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Medusa v2’s defineLink connects data models owned by different modules through a separate link table. Medusa’s documentation says that table stores the linked record IDs without database foreign-key constraints. That is a deliberate boundary for cross-module relationships—not evidence that Medusa removed foreign keys from every relationship in an application.

What `defineLink` does

Medusa uses a module link when an application needs to associate data models owned by different modules. Because module isolation prevents one module from directly adding a relationship to another module’s model, the link provides an association without changing the other module’s schema. The link definition lives in the application’s src/links directory and is exported with defineLink. Medusa describes this design in its module links guide.

As an Amazon Associate I earn from qualifying purchases.

For example, a Product model and a custom Blog Post model can be associated through a generated table such as product_product_blog_post, with columns such as product_id and post_id. Medusa states: “These columns store only the IDs of the linked records and do not hold a foreign key constraint.” This applies to the documented module-link table, not every table in a Medusa application.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A link can also carry information about the association itself. Medusa documents configurable cardinality through isList, query aliases, and custom columns for values such as metadata. Alias configuration is available since Medusa v2.17.2; that version note applies to the alias feature, not the origin of module links.

What changed—and what did not

The phrase “dropped the foreign keys” can suggest a broad schema change. The documented distinction is narrower: Medusa recommends relationships such as hasOne or belongsTo for models within the same module, and module links for models across modules. A same-module relationship can create a foreign-key column. Medusa’s data-model guide, for example, describes an email.user_id column with a foreign key to the user table: Data model relationships.

Medusa’s v2 migration guide frames module isolation as a way to integrate modules without side effects. Its example associates a custom Brand model with Product through a link rather than adding a brand column to Product’s entity. That is the stated architectural rationale, not proof that this pattern prevents every possible side effect: Migrate to Medusa v2.

Question Same-module relationship Cross-module `defineLink`
Where are the models owned? Within the same module. In different modules; the link preserves module isolation.
Is there a database foreign key? A relationship migration can add a foreign-key column; see Medusa’s relationship guide. The documented link-table ID columns do not have foreign-key constraints, according to Medusa’s module links guide.
How is the association represented? As a relationship between models in the module. As a separate link table holding linked record IDs.

What integrity checks does the Link API provide?

The absence of a database foreign key does not mean Medusa documents no checks at all. Its Link API describes cardinality behavior at the application level. Those rules are distinct from constraints enforced by the database engine, and their scope depends on the link type. See Medusa’s Link API reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • One-to-one: creating a conflicting second association causes an error.
  • One-to-many: the “many” side can link multiple records, while a record on the “one” side cannot be associated with a different record.
  • Many-to-many: Medusa says there are no integrity constraints preventing repeated links between the same pair.

That last detail matters when duplicate associations would be invalid for an application: the documented behavior does not promise a duplicate-pair constraint for many-to-many links.

Rank #3

How deletion and restoration work

Do not assume a database ON DELETE action on a module-link table. Medusa documents deletion behavior as part of the link lifecycle: cascade deletion is an explicit link option, and the Link.delete method can remove linked records whose definitions specify cascade deletion when a record is deleted through a workflow or module service. The Link API also documents restoring links for soft-deleted records.

In practice, the application’s deletion and restoration paths need to use the documented link operations and configuration appropriate to the desired behavior. The relevant operations are described in the Link API reference.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How link changes reach a database

For a self-hosted application, Medusa’s module-link guide says to run db:sync-links or db:migrate after adding or changing link definitions. Deployment practices remain relevant: Medusa Cloud’s database guide says deployments run pending database migrations, synchronize links, and then run pending data migration scripts. A self-hosted deployment should follow its own migration procedure. See the Medusa Cloud database guide.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Medusa’s Link guide notes that Remote Link was deprecated in favor of Link as of v2.2.0. This is an API-history note; it does not establish that cross-module links themselves began in that release.

Is `defineLink` a risky tradeoff?

It is a tradeoff in where relationship guarantees come from. The documented benefit is that modules retain ownership of their models while an application can associate their data. The cost is that the generated cross-module link table does not enforce those ID relationships with database foreign keys. Applications therefore need to account for the Link API’s documented cardinality behavior and explicitly configured lifecycle operations in the workflows that create, delete, or restore associations.

Medusa’s official documentation does not provide a benchmark, incident rate, or formal comparison of reliability or performance for this design. The documentation establishes the schema and API behavior above; it does not show that the design causes a measured reliability regression or performance penalty. Whether the tradeoff fits depends on the application’s need for module isolation and where it expects relationship integrity to be enforced.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.