You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+7-5Lines changed: 7 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -60,8 +60,8 @@ Imagine you have the following file in the package where your models are.
60
60
package models
61
61
62
62
typeUserstruct {
63
-
kallax.Model`table:"users"`
64
-
ID kallax.ULID`pk:""`
63
+
kallax.Model`table:"users" pk:"id"`
64
+
ID kallax.ULID
65
65
Usernamestring
66
66
Emailstring
67
67
Passwordstring
@@ -90,7 +90,7 @@ Sometimes you might want to use the generated code in the same package it is def
90
90
91
91
A model is just a Go struct that embeds the `kallax.Model` type. All the fields of this struct will be columns in the database table.
92
92
93
-
A model also needs to have one (and just one) primary key. That is whatever field of the struct with the struct tag `pk`, which can be `pk:""` for a non auto-incrementable primary key or `pk:"autoincr"` for one that is auto-incrementable.
93
+
A model also needs to have one (and just one) primary key. The primary key is defined using the `pk` struct tag on the `kallax.Model` embedding. You can also set the primary key in a field of the struct with the struct tag `pk`, which can be `pk:""` for a non auto-incrementable primary key or `pk:"autoincr"` for one that is auto-incrementable.
94
94
More about primary keys is discussed at the [primary keys](#primary-keys) section.
95
95
96
96
First, let's review the rules and conventions for model fields:
@@ -112,9 +112,9 @@ Let's see an example of models with all these cases:
112
112
113
113
```go
114
114
typeUserstruct {
115
-
kallax.Model`table:"users"`
115
+
kallax.Model`table:"users" pk:"id,autoincr"`
116
116
kallax.Timestamps
117
-
IDint64`pk:"autoincr"`
117
+
IDint64
118
118
Usernamestring
119
119
Passwordstring
120
120
Emails []string
@@ -146,6 +146,8 @@ type Metadata struct {
146
146
| Tag | Description | Can be used in |
147
147
| --- | --- | --- |
148
148
|`table:"table_name"`| Specifies the name of the table for a model. If not provided, the name of the table will be the name of the struct in lower snake case (e.g. `UserPreference` => `user_preference`) | embedded `kallax.Model`|
149
+
|`pk:"primary_key_column_name"`| Specifies the column name of the primary key. | embedded `kallax.Model`|
150
+
|`pk:"primary_key_column_name,autoincr"`| Specifies the column name of the autoincrementable primary key. | embedded `kallax.Model`|
149
151
|`pk:""`| Specifies the field is a primary key | any field with a valid identifier type |
150
152
|`pk:"autoincr"`| Specifies the field is an auto-incrementable primary key | any field with a valid identifier type |
151
153
|`kallax:"column_name"`| Specifies the name of the column | Any model field that is not a relationship |
0 commit comments