Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Reference

Every attribute of a model, and every key, in tables. The other pages of this chapter explain them with examples; the links lead there.

Django’s Meta options and their equivalents

Djangorind
db_table#[model(table = "…")] (required)
a field with primary_key=Truethe field id, or #[model(pk = "…")] (Primary keys)
orderingnone on the model: a resource’s #[ordering(.., default("…"))] (Filters), or an order_by in a scope
unique_together, UniqueConstraint#[unique(on(a, b))] (Indexes and constraints)
indexes, Index#[index(on(a, b))], or #[index(name, sql)]
constraints, CheckConstraint#[check(name = "…", sql = "…")]
abstract = Truerind::field_group! and #[rind::with_fields(..)] (Sharing fields)
a custom default manager#[model(scoped)] and the model’s own scopes (Scopes)
using, database routers#[model(database = "…")] (Databases)
help_textthe field’s doc comment
verbose_name, verbose_name_pluralnone
get_latest_by, default_related_name, permissionsnone

Django’s field options and their equivalents

Djangorind
null=TrueOption<T> (Fields)
blank=Truethe input’s business
max_length=#[column(max_length = n)]
default=, db_default=#[column(default = "SQL")] (Column options)
unique=True#[column(unique)]
db_index=True#[column(index)]
choices=the input’s choices(..), or an enum (Types of your own)
db_column=none: the field is named as the column
editable=Falseleave the field out of the input
auto_now, auto_now_addthe Tracking behaviour (Behaviours and hooks)
GeneratedField#[column(generated = "SQL")]

#[model(..)]

KeyMeaning
table = "…"the table’s name (required)
pk = "…"the field of the primary key, when it is not id
manual_keythe inserts give the key: no identity
key_name = "…"the name of the primary key’s constraint, when it is not <table>_pkey
tree = "…"the column of the parent: the model is a tree
scopedno default query: Query::new() does not compile (Scopes)
database = "…"the database the table is in, by the name the application gives it
behaviours(A, B)what every write of the model runs on its statement
hooks(A, B)what every write of an object runs after it (or before a deletion), in this order

#[column(..)]

KeyMeaning
max_length = 200the length of a text: character varying(200)
default = "'open'"the column’s default, in SQL
unique, unique = "…"the column is unique; with a name, the constraint’s name
index, index = "…"the column is indexed; with a name, the index’s name
sql = "geometry(Point,4326)"the SQL type, when rind cannot tell it
read = "ST_AsGeoJSON({})::jsonb"the SQL the column is read through, {} being the column
generated = "md5(params::text)"a generated column (GENERATED ALWAYS AS (…) STORED)

Rust types and SQL types

RustPostgreSQLNeeds sql = …
i64, i32, i16bigint, integer, smallint
f64, f32double precision, real
boolboolean
Stringtext, or character varying(n) with max_length
chrono::NaiveDatedate
chrono::DateTime<Utc>timestamp with time zone
chrono::NaiveDateTimetimestamp without time zone
serde_json::Valuejsonb
rind::db::types::Filetext, or character varying(n) with max_length
Vec<T>T[]
Option<T>T, nullable
Vec<u8>byteayes
rind::db::types::Decimalnumeric(p, s)yes
rind::db::types::Inetinetyes
rind::db::geo::Point, Polygon, MultiPolygon, Geometry, ShapePostGIS typesyes
rind::db::types::Vectorvector(n)yes
rind::db::types::TsVectortsvectoryes
rind::db::types::EnumValuean enumerated typeyes
a type of your ownanyyes (Types of your own)

#[fk(..)]

KeyMeaning
Target (first)the model the key points to
name = relationthe relation’s name, when it is not the field without _id
reverse = relationthe relation the target gets back, as a trait to import (Relations)
on_delete = …cascade, set_null or restrict; without it, the deletion is refused while the row points to it
in_codethe rule is applied by rind’s deletions, not by the database
deferredthe key is checked at commit (DEFERRABLE INITIALLY DEFERRED)
unindexedno index for the key
constraint = "…", index = "…"the names the constraint and its index already have (Migrations)
table = "…" (first), key = "…"a key to a table this crate cannot name (Organising an application)
externalwith table: the table has no model in the application
unfollowedwith table: no relation follows the key (models written from a database)

#[reverse(..)] and #[m2m(..)]

AttributeKeys
#[reverse(..)]name, model, fk = "<column of the other table>"; forward = relation gives the other model the relation along its key (Organising an application)
#[m2m(..)]name, model, through = "<link table>", from = "<its column to this model>", to = "<its column to the target>"

#[index(..)], #[unique(..)], #[check(..)]

AttributeKeys
#[index(..)]on(columns), or sql = "…" (what follows ON <table>) with name = "…"; unique (with sql only); name = "…"
#[unique(..)]on(columns), name = "…", nulls_not_distinct
#[check(..)]name = "…" and sql = "…", both required

Names rind gives

ObjectName
primary key<table>_pkey
unique column<table>_<column>_key
unique together<table>_<col1>_<col2>_key
index<table>_<columns>_idx
foreign key<table>_<column>_fkey, and its index <table>_<column>_idx
checkthe name you give

What the derive generates

For a model Station with a column city and a relation region:

ItemWhat it is
Station::city()a Path<String> to the column; an integer column is a Path<i64> whatever its width, a float a Path<f64>
Station::region()a path to the related model, from which its columns are reached
StationPaththe type of a path that ends at a Station; it has any, count, rows and filter for a many relation (Queries)
STATION_REGIONthe relation’s hop, <MODEL>_<RELATION>: what loaders and db.link take
StationColumnan enum with a variant per written column, for update
StationDepartures, StationPathDeparturesthe traits a reverse = departures on another model gives (<Target><Relation>)
Region::subtree() and the other tree functionsonly with tree (Trees)
insert, save, update, refresh, delete, delete_cascadethe object writes

A field whose name is a Rust keyword, r#type, is the column type and the path Model::type_().

Error messages of the derive

MistakeMessage
an unknown key in #[model(..)]unknown key: expected `table`, `pk`, `tree`, `database`, `scoped`, `manual_key`, `key_name`, `behaviours` or `hooks`
an unknown key in #[column(..)]unknown key: expected `max_length`, `sql`, `default`, `unique`, `index`, `read` or `generated`
a type rind does not know, without sqlcannot tell the SQL type of this field: give it, #[column(sql = "…")]
a foreign key field not ending in _ida foreign key field is named `<relation>_id`, or the relation is given: #[fk(Target, name = relation)]
a column name in on(..) that does not existthe model has no column `nope`
Query::new() on a scoped model`Loan` is a scoped model: it has no default query
a reverse relation used without its traitno associated function or constant named `notes` found, with a hint naming the trait to import