Skip to content

Commit 2e3859e

Browse files
authored
Update primary key definition docs
1 parent a7eb23f commit 2e3859e

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ Imagine you have the following file in the package where your models are.
6060
package models
6161

6262
type User struct {
63-
kallax.Model `table:"users"`
64-
ID kallax.ULID `pk:""`
63+
kallax.Model `table:"users" pk:"id"`
64+
ID kallax.ULID
6565
Username string
6666
Email string
6767
Password string
@@ -90,7 +90,7 @@ Sometimes you might want to use the generated code in the same package it is def
9090

9191
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.
9292

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.
9494
More about primary keys is discussed at the [primary keys](#primary-keys) section.
9595

9696
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:
112112

113113
```go
114114
type User struct {
115-
kallax.Model `table:"users"`
115+
kallax.Model `table:"users" pk:"id,autoincr"`
116116
kallax.Timestamps
117-
ID int64 `pk:"autoincr"`
117+
ID int64
118118
Username string
119119
Password string
120120
Emails []string
@@ -146,6 +146,8 @@ type Metadata struct {
146146
| Tag | Description | Can be used in |
147147
| --- | --- | --- |
148148
| `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` |
149151
| `pk:""` | Specifies the field is a primary key | any field with a valid identifier type |
150152
| `pk:"autoincr"` | Specifies the field is an auto-incrementable primary key | any field with a valid identifier type |
151153
| `kallax:"column_name"` | Specifies the name of the column | Any model field that is not a relationship |

0 commit comments

Comments
 (0)