1 ---
2 sidebar_label: "primary-key-required"
3 ---
4
5 export const metadata = {
6 title: `primary-key-required - ESLint plugin rules`,
7 }
8
9 # {metadata.title}
10
11 This rule requires that a data model defined with `model.define` declares a primary key via `.primaryKey()`.
12
13 ## Severity
14
15 `warn`. This rule is enabled in the [recommended](../../page.mdx#recommended) preset.
16
17 ## What it Targets
18
19 This rule targets data model files in your project's `src/modules` directory. It reports a `model.define` schema that doesn't declare a primary key on any of its properties.
20
21 The following code is reported by the rule:
22
23 ```ts title="src/modules/blog/models/post.ts"
24 import { model } from "@medusajs/framework/utils"
25
26 export const Post = model.define("post", {
27 id: model.id(),
28 title: model.text(),
29 })
30 ```
31
32 Instead, add `.primaryKey()` to one of the properties:
33
34 ```ts title="src/modules/blog/models/post.ts"
35 import { model } from "@medusajs/framework/utils"
36
37 export const Post = model.define("post", {
38 id: model.id().primaryKey(),
39 title: model.text(),
40 })
41 ```
42
43 ## Why it's Important
44
45 A data model needs a primary key to uniquely identify each record. Without one, the model can't be persisted or queried reliably.
46
47 Learn more in the [Data Models documentation](!docs!/learn/fundamentals/data-models).
48
49 ## Fixable
50
51 This rule isn't auto-fixable.
52
53 ## Turn it Off
54
55 To turn off this rule, set it to `off` in your ESLint configuration:
56
57 ```ts title="eslint.config.ts"
58 import { defineConfig } from "eslint/config"
59 import medusa from "@medusajs/eslint-plugin"
60
61 export default defineConfig([
62 ...medusa.configs.recommended,
63 {
64 rules: {
65 "@medusajs/primary-key-required": "off",
66 },
67 },
68 ])
69 ```
70
71 Or disable it for a single line using an inline comment:
72
73 ```ts
74 // eslint-disable-next-line @medusajs/primary-key-required
75 export const Post = model.define("post", {
76 ```