1 ---
2 sidebar_label: "read-only-link-requires-field"
3 ---
4
5 export const metadata = {
6 title: `read-only-link-requires-field - ESLint plugin rules`,
7 }
8
9 # {metadata.title}
10
11 This rule requires read-only `defineLink` calls to specify a `field` on the first linkable, and a `primaryKey` or `alias` on the inverse form.
12
13 ## Severity
14
15 `error`. This rule is enabled in the [recommended](../../page.mdx#recommended) preset.
16
17 ## What it Targets
18
19 This rule targets files in your project's `src/links` directory. It reports read-only `defineLink` calls (calls whose options object sets `readOnly: true`) whose first argument is missing a `field` property. For the inverse form, where the second argument spreads `<Module>.linkable.<dataModel>.id`, it also requires either a `primaryKey` property or an `alias` property.
20
21 The following code is reported by the rule:
22
23 ```ts title="src/links/post-product.ts"
24 import { defineLink } from "@medusajs/framework/utils"
25 import BlogModule from "../modules/blog"
26 import ProductModule from "@medusajs/medusa/product"
27
28 export default defineLink(
29 BlogModule.linkable.post,
30 ProductModule.linkable.product,
31 { readOnly: true }
32 )
33 ```
34
35 Instead, specify the `field` that stores the linked record's ID on the first argument:
36
37 ```ts title="src/links/post-product.ts"
38 import { defineLink } from "@medusajs/framework/utils"
39 import BlogModule from "../modules/blog"
40 import ProductModule from "@medusajs/medusa/product"
41
42 export default defineLink(
43 {
44 linkable: BlogModule.linkable.post,
45 field: "product_id",
46 },
47 ProductModule.linkable.product,
48 { readOnly: true }
49 )
50 ```
51
52 For the inverse form, include a `primaryKey` on the second argument:
53
54 ```ts title="src/links/post-product.ts"
55 export default defineLink(
56 { linkable: ProductModule.linkable.product, field: "id" },
57 {
58 ...BlogModule.linkable.post.id,
59 primaryKey: "product_id",
60 },
61 { readOnly: true }
62 )
63 ```
64
65 Alternatively, you can use `alias` instead of `primaryKey` on the inverse form when re-aliasing a linkable. For example, when linking to draft orders using the `order` linkable re-aliased to `draft_order`:
66
67 ```ts title="src/links/quote-draft-order.ts"
68 import { defineLink } from "@medusajs/framework/utils"
69 import QuoteModule from "../modules/quote"
70 import OrderModule from "@medusajs/medusa/order"
71
72 export default defineLink(
73 { linkable: QuoteModule.linkable.quote, field: "draft_order_id" },
74 { ...OrderModule.linkable.order.id, alias: "draft_order" },
75 { readOnly: true }
76 )
77 ```
78
79 ## Why it's Important
80
81 A read-only link doesn't store its own data, so Medusa needs the `field` to know where the linked record's ID lives. Without it, the link can't resolve the related records.
82
83 Learn more in the [Read-Only Module Links documentation](!docs!/learn/fundamentals/module-links/read-only).
84
85 ## Fixable
86
87 This rule isn't auto-fixable.