Primary keys
Every model has one primary key, made of one column. This page shows the
usual key, id, and the other cases: a key with another name, a key the
application gives, a key that is not a number.
The usual key: id
#![allow(unused)]
fn main() {
#[derive(Debug, Clone, Model)]
#[model(table = "lab_tag")]
pub struct Tag {
pub id: i64,
pub name: String,
}
}
CREATE TABLE "lab_tag" (
"id" bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"name" text NOT NULL
);
A field called id is the primary key. Its column is an identity: the
database gives each new row the next number. This is Django’s
BigAutoField, the default of new Django projects.
Unlike Django, the field is written in the struct: rind adds no field of
its own. Use i64. An i32 key works too (it is an integer identity),
but the foreign keys that point to it must then be i32 too, and the
rest of rind (resources, deletions that follow keys) is written for
i64 keys.
Inserts do not send it. An identity key is left out of every insert, and the database’s value comes back:
#![allow(unused)]
fn main() {
let tag = Tag { id: 0, name: "rust".into() }.insert(&db).await?;
// Tag { id: 1, name: "rust" }: the 0 was not sent
}
The id: 0 in the struct is only a placeholder; any value would do.
GENERATED BY DEFAULT (and not ALWAYS) lets an insert written in SQL
give a key all the same, for an import or a test fixture:
INSERT INTO catalog_author (id, name) VALUES (1, 'Frank Herbert');
After such inserts, the identity’s counter does not know the keys you gave. Move it past them, or the next insert through rind fails on a duplicate key:
SELECT setval(pg_get_serial_sequence('catalog_author', 'id'), (SELECT max(id) FROM catalog_author));
A key with another name: pk
#![allow(unused)]
fn main() {
#[derive(Debug, Clone, Model)]
#[model(table = "lab_k3", pk = "number")]
pub struct K3 {
pub number: i32,
}
}
CREATE TABLE "lab_k3" (
"number" integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY
);
pk = "…" names the field of the key, when it is not id: Django’s
primary_key=True on another field. The key is still an identity, unless
the model also says manual_key.
A key the application gives: manual_key
Sometimes the key is not a counter: a country’s ISO code, a key copied
from another system, a key shared with another table. Say so with
manual_key:
#![allow(unused)]
fn main() {
#[derive(Debug, Clone, Model)]
#[model(table = "lab_country", pk = "code", manual_key)]
pub struct Country {
#[column(max_length = 2)]
pub code: String,
pub name: String,
}
}
CREATE TABLE "lab_country" (
"code" character varying(2) PRIMARY KEY,
"name" text NOT NULL
);
There is no identity, and inserts write the key:
#![allow(unused)]
fn main() {
let fr = Country { code: "FR".into(), name: "France".into() }.insert(&db).await?;
}
manual_key works with an i64 id too:
#![allow(unused)]
fn main() {
#[model(table = "lab_k2", manual_key)]
pub struct K2 { pub id: i64 }
// "id" bigint PRIMARY KEY
}
Watch out: a key that is not an integer must have
manual_key. Without it, the migration says"code" text GENERATED BY DEFAULT AS IDENTITY, which PostgreSQL refuses when the migration is applied. The derive does not stop it yet.
A foreign key to a model with a text key is an Option<String> or a
String:
#![allow(unused)]
fn main() {
#[fk(Country)]
pub country_id: Option<String>,
// ALTER TABLE … ADD CONSTRAINT "lab_article_country_id_fkey" FOREIGN KEY ("country_id") REFERENCES "lab_country" ("code");
}
Some features need an i64 key: deletions that follow foreign
keys (delete_cascade),
many-to-many links (db.link), and the loaders that read
a page’s relations. Prefer an i64 id and a unique code, unless you
describe a database that already has another key.
The name of the key’s constraint: key_name
PostgreSQL names the primary key’s constraint <table>_pkey. A database
made by another tool may have another name; describe it with key_name,
so that rind’s migrations and checks match it:
#![allow(unused)]
fn main() {
#[model(table = "lab_country", pk = "code", manual_key, key_name = "lab_country_code_pk")]
// "code" character varying(2) CONSTRAINT "lab_country_code_pk" PRIMARY KEY,
}
Keys of several columns
A primary key of several columns (Django 5.2’s CompositePrimaryKey) is
not supported. Give the table an id, and make the columns
unique together.
Summary
| You want | Write | Column |
|---|---|---|
| the usual key | pub id: i64 | bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY |
| another name | #[model(pk = "number")] | an identity on that column |
| a key you give | #[model(manual_key)] | PRIMARY KEY, no identity |
| a text key | #[model(pk = "code", manual_key)] | character varying(n) PRIMARY KEY |
| a constraint’s own name | #[model(key_name = "…")] | CONSTRAINT "…" PRIMARY KEY |
Next: Foreign keys.