Skip to main content

umbral_core/orm/
model.rs

1//! The `Model` trait: the abstraction every umbral model implements.
2//!
3//! At M2 the trait is implemented by hand (`impl Model for Post` lives
4//! in `post.rs`). At M3 the same impl is generated by a
5//! `#[derive(Model)]` proc macro. M4 hooks into `FIELDS` for the
6//! field/backend compatibility system check. M5 hooks into `FIELDS` for
7//! the migration engine's snapshot diff. The trait is intentionally
8//! narrow at M2 — primary-key type, table name, field metadata, and
9//! that's it.
10//!
11//! Through Phase 2 of the Postgres rollout `Model` carried
12//! `for<'r> sqlx::FromRow<'r, SqliteRow>` as a supertrait so the
13//! QuerySet terminals could blanket on `T: Model`. Phase 2.5 drops
14//! that supertrait: the user struct still uses `#[derive(sqlx::FromRow)]`
15//! (which emits a generic `impl<'r, R: Row> FromRow<'r, R>` covering
16//! both SQLite and Postgres rows), and the QuerySet terminals carry
17//! the FromRow bound on the method, not the trait — so the same
18//! `Manager<T>` works on either backend.
19//!
20//! See `docs/specs/04-orm-model-and-fields.md` for the target shape and
21//! the M2→M3→M4→M5 progression.
22
23/// Trait for eagerly hydrating `ForeignKey<U>.resolved` fields by name.
24///
25/// `#[derive(Model)]` emits this impl for every model. Models with no FK
26/// fields get a no-op impl; models with FK fields get a `hydrate_fk` body
27/// that matches on `field_name` and a `fk_id_for` body that returns the raw
28/// FK integer for a named field.
29///
30/// The `select_related` machinery in `QuerySet` calls these two methods in
31/// sequence: first `fk_id_for` to collect the IDs to batch-fetch, then
32/// `hydrate_fk` with the fetched JSON to populate `ForeignKey<U>.resolved`.
33///
34/// `U` must implement `serde::Deserialize` for `hydrate_fk` to succeed.
35/// All umbral models already derive `Deserialize`, so this bound is always
36/// satisfied in practice.
37pub trait HydrateRelated {
38    /// Return the raw FK value stored in the field named `field_name`,
39    /// or `None` if the field doesn't exist on this model or is not a FK.
40    ///
41    /// Used by `select_related` to collect all FK ids from the result
42    /// set before running the batch `IN (...)` lookup.
43    ///
44    /// PK lift Pass D: returns `Option<serde_json::Value>` (was
45    /// `Option<i64>`) so FK targets keyed by `String` / `Uuid` /
46    /// composite codename flow through the typed select_related
47    /// path. The macro emits `serde_json::to_value(self.<field>.id())`
48    /// — works for any `Serialize` PK type without per-target
49    /// specialization. Integer-PK targets carry through as
50    /// `Value::Number`; the existing i64 hot path is unchanged at
51    /// the JSON layer.
52    fn fk_id_for(&self, field_name: &str) -> Option<serde_json::Value>;
53
54    /// Set `ForeignKey<U>.resolved` for the field named `field_name` by
55    /// deserialising `row` as the target model type.
56    ///
57    /// A field name that doesn't match any FK on this model is silently
58    /// ignored (a noop). Deserialisation errors are also silently swallowed —
59    /// the FK keeps its raw-integer form without a resolved object. This
60    /// is intentional: a bad `select_related` name is a
61    /// programming error caught in tests, not a runtime panic.
62    fn hydrate_fk(&mut self, field_name: &str, row: &serde_json::Value);
63
64    /// Set the `parent_id` cache on every `M2M<U>` field this model
65    /// owns. Closes the second BUG-16 gap: without this, `m2m.add(&t)`
66    /// silently writes a junction row with `parent_id = 0` because the
67    /// macro skips M2M fields in the `FromRow` decode path.
68    ///
69    /// Called by QuerySet terminals after each row is decoded. The
70    /// macro emits a body that walks the model's M2M fields and calls
71    /// `set_parent_id(self.<pk>)` on each — so loading a `Group`
72    /// gives every `M2M<U>` slot on it the right `parent_id` to write
73    /// against.
74    ///
75    /// Default: no-op. The macro-emitted body shadows this for any
76    /// model that declares an M2M field. A model with no M2M fields
77    /// inherits the default and pays nothing.
78    ///
79    /// PK-agnostic: the macro sets each `M2M<Child, P>` field's parent id
80    /// from `self.<pk>` via the typed [`crate::orm::M2M::set_parent_id`],
81    /// so it works for **any** parent PK type — `i64`, `String`,
82    /// `uuid::Uuid`. A non-i64-PK parent declares the field with the
83    /// matching `P` (e.g. `M2M<Student, String>`); `P` defaults to `i64`.
84    fn set_m2m_parent_ids(&mut self) {}
85
86    /// Return this row's primary key as a `serde_json::Value`, whatever
87    /// the PK type — `i64`, `String`, `uuid::Uuid`, a custom newtype.
88    /// The relation-hydration paths (`prefetch_related`, reverse-FK and
89    /// reverse-OneToOne collection) bucket children by the parent's PK,
90    /// and keying those buckets on a `Value` (canonicalised via
91    /// [`crate::orm::pk_key`]) lets UUID- and slug-PK models flow through
92    /// too, not just i64.
93    ///
94    /// Default: `None`. The `#[derive(Model)]` macro emits an override for
95    /// every model that returns `to_value(&self.<pk>)` — so a hand-written
96    /// `Model` impl that doesn't override it simply opts out of the
97    /// Value-keyed hydration (a forgive-and-skip posture).
98    fn pk_as_json(&self) -> Option<serde_json::Value> {
99        None
100    }
101
102    /// Attach a list of pre-fetched child rows to the named `M2M<U>`
103    /// field's `resolved` slot. Called by `QuerySet::prefetch_related`
104    /// (gap #19) after a batched JOIN through the junction table
105    /// returns one Vec<U> per parent.
106    ///
107    /// `rows` carries the child rows as JSON objects ready for
108    /// `serde_json::from_value::<U>(...)`. Decoding failures (e.g. a
109    /// row that doesn't match the target struct shape) silently drop
110    /// that one row from the resolved set — same forgive-and-continue
111    /// posture as `hydrate_fk` for `select_related`.
112    ///
113    /// A field name that doesn't match any M2M field on this model is
114    /// a no-op. The macro-emitted body pattern-matches the M2M fields
115    /// declared on this struct; the default below is empty so models
116    /// without M2M fields pay nothing.
117    fn set_m2m_resolved_json(&mut self, _field_name: &str, _rows: Vec<serde_json::Value>) {}
118
119    /// Gap #44 — attach a list of pre-fetched child rows to the
120    /// named `ReverseSet<C>` field's `resolved` slot. Counterpart
121    /// to `set_m2m_resolved_json` but for reverse-FK collections
122    /// (one parent, many children pointing at it via a FK column).
123    ///
124    /// Called by `QuerySet::prefetch_related` after the batched
125    /// `SELECT * FROM <child> WHERE <fk_col> IN (parent_pks)` query
126    /// returns child rows grouped by `<fk_col>` value.
127    ///
128    /// `rows` carries the child rows as JSON objects ready for
129    /// `serde_json::from_value::<C>(...)`. Decoding failures
130    /// silently drop that one row — same forgive-and-continue
131    /// posture as the M2M variant.
132    ///
133    /// A field name that doesn't match any `ReverseSet` field on
134    /// this model is a no-op. The macro-emitted body pattern-
135    /// matches the ReverseSet fields declared on this struct; the
136    /// default below is empty so models without reverse-FK fields
137    /// pay nothing.
138    fn set_reverse_fk_resolved_json(&mut self, _field_name: &str, _rows: Vec<serde_json::Value>) {}
139
140    /// Reverse-OneToOne counterpart to
141    /// `set_reverse_fk_resolved_json`. Called by `prefetch_related`
142    /// with `Some(child_json)` when the runtime FK lookup found
143    /// exactly one matching child, or `None` when no child matched
144    /// (the slot still flips `is_loaded()` to `true`).
145    ///
146    /// Default: no-op. The macro emits per-field arms for any model
147    /// declaring `pub <name>: OneToOne<C>` fields.
148    fn set_one_to_one_resolved_json(&mut self, _field_name: &str, _row: Option<serde_json::Value>) {
149    }
150
151    /// Move form-staged M2M pending ids from `self` into `dest`,
152    /// field by field. The typed `create()` builds its INSERT from the
153    /// caller's instance, then reads a *fresh* row back from the DB
154    /// (carrying the autoincremented PK) — the pending ids staged by the
155    /// Form derive live on the caller's instance, not the readback row.
156    /// This hook transfers them across so `write_pending_m2m` on the
157    /// readback row (which has the real parent_id seeded) finds them.
158    /// Default: no-op for models with no M2M fields.
159    fn take_pending_m2m_into(&mut self, _dest: &mut Self) {}
160
161    /// Flush form-staged M2M selections to their junction tables after
162    /// the parent row was inserted. The macro emits a body that walks
163    /// this model's M2M fields, reads `parent_id` + `junction_table`
164    /// (seeded by `set_m2m_parent_ids`) and the pending child ids, and
165    /// calls `set_junction_dynamic`. Default: no-op for models with no
166    /// M2M fields.
167    ///
168    /// Async + boxed (rather than `#[async_trait]` on the whole trait)
169    /// so `HydrateRelated`'s existing non-async methods stay as they
170    /// are. Junction writes hit the DB, so this is kept off the hot
171    /// decode path — only the typed `create()` calls it.
172    fn write_pending_m2m<'a>(
173        &'a mut self,
174    ) -> std::pin::Pin<
175        Box<
176            dyn std::future::Future<Output = Result<(), crate::orm::write::WriteError>> + Send + 'a,
177        >,
178    > {
179        Box::pin(async { Ok(()) })
180    }
181}
182
183/// The trait every model implements.
184///
185/// Read at runtime to build queries (`T::TABLE`, `T::FIELDS`), at boot
186/// to validate field/backend compatibility (M4), and at migration time
187/// to diff against the last snapshot (M5).
188///
189/// `Model` is metadata-only — it carries no row-materialization bound.
190/// QuerySet terminals add `for<'r> FromRow<'r, R>` for the row type
191/// they need at the call site (sqlite or postgres). User structs pick
192/// up both impls via a single `#[derive(sqlx::FromRow)]` because
193/// sqlx's derive emits a generic-over-`R` impl.
194pub trait Model: Sized + Send + Sync + Unpin + 'static {
195    /// The primary-key type. M2 supports `i64` only; UUID lands later.
196    type PrimaryKey: PrimaryKey;
197
198    /// The struct name, used by the migration engine (M5) to label
199    /// snapshot entries and to map autodetected operations back to
200    /// the model that produced them. The M3 derive emits the struct
201    /// ident verbatim ("Post", "Comment", etc.).
202    const NAME: &'static str;
203
204    /// The SQL table name. M3's derive defaults this to the
205    /// `snake_case` of the struct name unless `#[umbral(table = "...")]`
206    /// overrides it.
207    const TABLE: &'static str;
208
209    /// The app label (the owning plugin's name) this model belongs to.
210    ///
211    /// Sourced from `#[umbral(plugin = "...")]`; defaults to `"app"` (the
212    /// registry's default key) when the attribute is absent. Authoritative
213    /// for permission codenames (gaps2 #80g): `umbral-permissions` reads this
214    /// to build `<app_label>.<verb>_<model>` codenames, instead of splitting
215    /// the table name at the first `_` (which collided distinct models).
216    const APP_LABEL: &'static str = "app";
217
218    /// Static metadata for every field on the model.
219    ///
220    /// One [`FieldSpec`] per field, in declaration order. Read by the
221    /// QuerySet (to build the SELECT column list), by the system check
222    /// (M4) for field/backend compatibility, and by the migration
223    /// engine (M5) for snapshot diffing.
224    const FIELDS: &'static [FieldSpec];
225
226    /// Human-readable display name for this model, used by the admin
227    /// sidebar as the default label. Defaults to `Self::NAME`.
228    ///
229    /// Override via `#[umbral(display = "Users")]` on the struct.
230    const DISPLAY: &'static str = Self::NAME;
231
232    /// Lucide icon slug shown next to this model in the admin sidebar.
233    /// Defaults to `"database"`. Any valid Lucide icon name works; unknown
234    /// names are silently ignored by Lucide at render time.
235    ///
236    /// Override via `#[umbral(icon = "users")]` on the struct.
237    const ICON: &'static str = "database";
238
239    /// Database alias this model lives on, when the app registers more
240    /// than one pool via `AppBuilder::database(...)`. `None` (the
241    /// default) means "use whatever the owning plugin chose via
242    /// `Plugin::database()`, or `\"default\"` if neither side
243    /// overrode."
244    ///
245    /// Override via `#[umbral(database = "analytics")]` on the struct.
246    /// Per-model wins over per-plugin — useful for a single plugin
247    /// that owns one model on the primary DB and another on an
248    /// archive/analytics DB.
249    const DATABASE: Option<&'static str> = None;
250
251    /// Single-row-marker. When `true`, the admin auto-redirects the
252    /// list view to the (sole) row's edit form, hides the "+ New"
253    /// button, and surfaces the model as a settings-style screen.
254    /// The single-row settings model pattern. Set via
255    /// `#[umbral(singleton)]` on the struct. Closes BUG-9 in
256    /// `bugs/tests/testBugs.md`.
257    ///
258    /// Default `false`. Default-row seeding (so the first admin
259    /// visit doesn't 404) is the user's responsibility — typically
260    /// a one-liner in `Plugin::on_ready` that calls
261    /// `T::objects().create(T::default()).await` if the count is
262    /// zero. A future framework helper could automate that; for v1
263    /// the trait const is enough to let admin and any third-party
264    /// tool know the model is singleton-shaped.
265    const SINGLETON: bool = false;
266
267    /// Feature #72 — soft-delete marker. Set via
268    /// `#[umbral(soft_delete)]` on the struct. When true, the
269    /// framework treats this model as having a `deleted_at:
270    /// Option<DateTime<Utc>>` column (which the user MUST declare
271    /// — derive macros can't add fields to the input struct), and:
272    ///
273    /// - Every `QuerySet<T>` terminal auto-injects
274    ///   `WHERE deleted_at IS NULL` so soft-deleted rows are
275    ///   invisible by default.
276    /// - `Manager::delete_instance(&row)` and `QuerySet::delete()`
277    ///   issue `UPDATE table SET deleted_at = NOW() WHERE ...`
278    ///   instead of a hard `DELETE FROM table WHERE ...`.
279    /// - Callers who actually want the soft-deleted rows (admin
280    ///   trash views, audit dumps, undelete flows) opt back in
281    ///   per-query via `.with_deleted()` or `.only_deleted()`.
282    /// - Callers who need a hard DELETE (GDPR purge, etc.) use
283    ///   `.hard_delete()` to bypass the soft path on a per-call
284    ///   basis.
285    ///
286    /// Default false so existing models compile unchanged.
287    const SOFT_DELETE: bool = false;
288
289    /// Composite-UNIQUE constraints. Each inner slice names a
290    /// constraint over the listed column names. Set via
291    /// `#[umbral(unique_together = [["a", "b"]])]`. Closes BUG-6 in
292    /// `bugs/tests/testBugs.md`. Default empty; the migration engine
293    /// emits one `UNIQUE (col1, col2)` clause per inner group on
294    /// `CREATE TABLE`.
295    const UNIQUE_TOGETHER: &'static [&'static [&'static str]] = &[];
296
297    /// Multi-column indexes. Each inner slice names an index over
298    /// the listed columns. Set via
299    /// `#[umbral(indexes = [["tenant_id", "created_at"]])]`. Closes
300    /// BUG-7. Default empty; the migration engine emits
301    /// `CREATE INDEX IF NOT EXISTS idx_<table>_<col1>_<col2>` after
302    /// the `CREATE TABLE`. Single-column indexes stay on the field
303    /// attribute (`#[umbral(index)]`).
304    const INDEXES: &'static [&'static [&'static str]] = &[];
305
306    /// Default `ORDER BY` clause, applied when a QuerySet terminates
307    /// without an explicit `order_by`. Each tuple is `(column_name,
308    /// is_descending)`. Set via
309    /// `#[umbral(ordering = ["-published_at", "id"])]` (leading `-`
310    /// flips to DESC). Closes BUG-8. Default empty.
311    const ORDERING: &'static [(&'static str, bool)] = &[];
312
313    /// Field names to STRIP from signal payloads (audit_2 core-app-config #10).
314    /// Set per-field via `#[umbral(signal_skip)]`. The ORM signal emitters
315    /// (`pre/post_save`, `pre/post_delete`, `pre/post_update`) serialize the
316    /// whole row into the `"instance"` payload that fans out to every
317    /// subscriber; a subscriber that logs or persists payloads (the natural
318    /// audit-log shape) would otherwise copy password hashes, tokens, and PII
319    /// into logs / secondary stores. Listed fields are removed from the
320    /// serialized instance before it is emitted. Default empty (full row).
321    const SIGNAL_SKIP_FIELDS: &'static [&'static str] = &[];
322
323    /// Many-to-many relations declared on this model. Each entry names
324    /// a field and its target model. The migration engine uses this to
325    /// auto-generate junction tables; the admin uses it to render M2M
326    /// pickers. Default empty.
327    const M2M_RELATIONS: &'static [M2MRelationSpec] = &[];
328
329    /// Gap #44 — reverse-FK collections declared on this model via
330    /// `#[umbral(reverse_fk = "<fk_col>")] pub <name>: ReverseSet<C>`.
331    /// Each entry tells `prefetch_related` how to fetch the children:
332    /// `SELECT * FROM <target_table> WHERE <fk_column> IN (parent_pks)`
333    /// then group by `<fk_column>` value, populate each parent's
334    /// `ReverseSet.resolved`. Default empty; the macro emits one
335    /// entry per declared `ReverseSet<C>` field.
336    const REVERSE_FK_RELATIONS: &'static [ReverseFkRelationSpec] = &[];
337
338    /// Reverse OneToOne accessors declared on this model via
339    /// `pub <name>: OneToOne<C>` (no umbral attribute required).
340    /// Unlike `REVERSE_FK_RELATIONS`, the FK column on the child is
341    /// not named at macro time — `prefetch_related` looks it up at
342    /// runtime by scanning the child's `FIELDS` for the UNIQUE FK
343    /// pointing back at this model's table. Exactly one match
344    /// required; 0 or 2+ matches surface a loud error naming the
345    /// ambiguity.
346    const ONE_TO_ONE_RELATIONS: &'static [OneToOneRelationSpec] = &[];
347
348    /// Return the primary key of this instance.
349    fn primary_key(&self) -> Self::PrimaryKey;
350}
351
352/// Static metadata for one many-to-many relation declared on a model.
353///
354/// Carried by `Model::M2M_RELATIONS`. The migration engine uses this
355/// to emit `CREATE TABLE` for the junction table; the admin uses it
356/// to know which fields render as multi-select pickers.
357#[derive(Debug, Clone, PartialEq, Eq)]
358pub struct M2MRelationSpec {
359    /// The Rust field name (e.g. `"tags"`).
360    pub field_name: &'static str,
361    /// The target model's table name (e.g. `"tag"`).
362    pub target_table: &'static str,
363    /// The target model's struct name (e.g. `"Tag"`). Used for reverse
364    /// accessor lookups and OpenAPI schema references.
365    pub target_name: &'static str,
366}
367
368/// Static metadata for one reverse OneToOne field on a model. The
369/// FK column on the child is intentionally omitted — `prefetch_related`
370/// resolves it at runtime by scanning the child's `FIELDS` for the
371/// UNIQUE FK pointing back at `target_table`. Carried by
372/// [`Model::ONE_TO_ONE_RELATIONS`].
373///
374/// Example: `pub struct User { pub profile: OneToOne<Profile>, ... }`
375/// emits one entry: `{ field_name: "profile", target_table:
376/// "profile", target_name: "Profile" }`. At prefetch time the loader
377/// finds the column on Profile (`pub user: ForeignKey<User>` with
378/// `#[umbral(unique)]`) and issues `SELECT * FROM profile WHERE user
379/// IN (parent_pks)`.
380#[derive(Debug, Clone, PartialEq, Eq)]
381pub struct OneToOneRelationSpec {
382    /// The Rust field name on the parent (e.g. `"profile"`).
383    pub field_name: &'static str,
384    /// The child model's table name (e.g. `"profile"`).
385    pub target_table: &'static str,
386    /// The child model's struct name (e.g. `"Profile"`). Reserved
387    /// for symmetry with `M2MRelationSpec` / `ReverseFkRelationSpec`.
388    pub target_name: &'static str,
389}
390
391/// Static metadata for one reverse-FK collection field on a model
392/// (gap #44). Carried by `Model::REVERSE_FK_RELATIONS`.
393///
394/// Example: `pub struct Post` with
395/// `#[umbral(reverse_fk = "post")] pub comment_set: ReverseSet<Comment>`
396/// emits one entry: `{ field_name: "comment_set", target_table:
397/// "comment", target_name: "Comment", fk_column: "post" }`.
398///
399/// `prefetch_related("comment_set")` uses this to issue
400/// `SELECT * FROM comment WHERE post IN (parent_pks)` then group
401/// rows by `post` value, populating each parent's `ReverseSet`.
402#[derive(Debug, Clone, PartialEq, Eq)]
403pub struct ReverseFkRelationSpec {
404    /// The Rust field name on the parent (e.g. `"comment_set"`).
405    pub field_name: &'static str,
406    /// The child model's table name (e.g. `"comment"`).
407    pub target_table: &'static str,
408    /// The child model's struct name (e.g. `"Comment"`). Reserved
409    /// for symmetry with `M2MRelationSpec`.
410    pub target_name: &'static str,
411    /// Name of the FK column on the child that points back at the
412    /// parent (e.g. `"post"`). The prefetch loader filters on this
413    /// column: `WHERE <fk_column> IN (parent_pks)`.
414    pub fk_column: &'static str,
415    /// Mirrors the CHILD model's `Model::SOFT_DELETE`. `annotate_count`
416    /// folds `AND <child>.deleted_at IS NULL` into the correlated
417    /// count subquery when this is `true`, so a trashed child stops
418    /// inflating the parent's count. Filled by the Model derive from
419    /// `<Child as Model>::SOFT_DELETE`.
420    pub soft_delete: bool,
421}
422
423/// Types that can serve as a model's primary key.
424///
425/// Built-in impls cover the integer widths sea-query has native
426/// `Value` variants for (i8 / i16 / i32 / i64, u8 / u16 / u32 / u64),
427/// `uuid::Uuid`, and `String` (for slug-style keys). The bound is
428/// `Clone + Send + Sync + 'static + Into<sea_query::Value>` — the
429/// `Into<Value>` requirement lets the M2M junction-table CRUD path
430/// bind the PK through sea-query without a per-type adapter, on both
431/// SQLite and Postgres. Closes BUG-16 phase 2.
432///
433/// 128-bit integers (`i128` / `u128`) are deliberately not in the
434/// catalogue: sea-query's `Value` enum has no native variant for them
435/// and neither shipped backend exposes a 128-bit integer column type.
436/// Use `i64` or `String` instead.
437///
438/// User crates extend the catalogue with one line as long as the
439/// custom type already lowers to a `sea_query::Value`:
440///
441/// ```ignore
442/// #[derive(Clone)]
443/// pub struct UserId(pub u64);
444///
445/// impl From<UserId> for sea_query::Value {
446///     fn from(id: UserId) -> Self { id.0.into() }
447/// }
448/// impl umbral::orm::PrimaryKey for UserId {}
449/// ```
450pub trait PrimaryKey:
451    Clone + Send + Sync + 'static + Into<sea_query::Value> + std::fmt::Display
452{
453}
454
455// Integer widths sea-query has Value variants for. Postgres exposes
456// SMALLINT / INT / BIGINT for the signed half; the unsigned widths
457// upcast (sea-query lowers u8/u16/u32 to the next signed width, u64
458// to BIGINT, matching what both backends actually store).
459impl PrimaryKey for i8 {}
460impl PrimaryKey for i16 {}
461impl PrimaryKey for i32 {}
462impl PrimaryKey for i64 {}
463impl PrimaryKey for u8 {}
464impl PrimaryKey for u16 {}
465impl PrimaryKey for u32 {}
466impl PrimaryKey for u64 {}
467
468// Non-integer built-ins. UUIDs and slug-style String keys are the
469// two non-integer shapes the porting catalogue calls out.
470impl PrimaryKey for uuid::Uuid {}
471impl PrimaryKey for String {}
472
473/// Static metadata for one column on a model.
474///
475/// Constructed once per field as a const, lives in `Model::FIELDS`.
476/// Carries enough information for the QuerySet, the system check, and
477/// the migration engine to do their jobs without the model needing any
478/// runtime introspection.
479#[derive(Debug, Clone, Copy, PartialEq, Eq)]
480pub struct FieldSpec {
481    /// The SQL column name — always the Rust field name. Only the table name
482    /// is overridable (via `#[umbral(table = "...")]`); there is no field-level
483    /// column-rename attribute, so the column is whatever the field is called.
484    pub name: &'static str,
485
486    /// The SQL type kind. M2 ships the minimum set needed for the
487    /// hardcoded `Post` model (`BigInt`, `Text`, `Timestamptz`);
488    /// additional variants land as the M3 derive's field-type
489    /// catalogue grows.
490    pub ty: SqlType,
491
492    /// Whether the column is part of the primary key.
493    pub primary_key: bool,
494
495    /// Whether the column accepts SQL NULL. Maps from `Option<T>` in
496    /// the struct definition; the only path to NULL is `Option<T>`,
497    /// per the `04-orm-model-and-fields.md` invariant.
498    pub nullable: bool,
499
500    /// Which backends this field type works on. Empty slice means "all
501    /// backends." Non-empty restricts the field to those listed; the
502    /// M4 boot system check rejects models that use a field on an
503    /// unsupported backend.
504    pub supported_backends: &'static [&'static str],
505
506    /// For `SqlType::ForeignKey` fields: the SQL table name of the
507    /// referenced model (i.e. `T::TABLE`). The migration engine reads
508    /// this at DDL-emit time to produce `REFERENCES "<target>"("id")`.
509    /// `None` for all non-FK fields.
510    pub fk_target: Option<&'static str>,
511
512    /// When `true`, this field is never rendered on any form (create or
513    /// edit) AND the REST plugin drops it from POST/PUT/PATCH request
514    /// bodies before write. This is the framework's "server-managed,
515    /// never accepts client input" flag — `password_hash`,
516    /// `internal_token`, audit timestamps the database owns.
517    /// Set via `#[umbral(noform)]`.
518    ///
519    /// OpenAPI emits `readOnly: true` for `noform` columns so Swagger
520    /// UI / generated clients honour the contract too. If you only
521    /// want the admin to render the field disabled — without affecting
522    /// the REST API or the spec — use `noedit` below.
523    ///
524    /// If `noform` is true, `noedit` is moot (noform takes precedence).
525    pub noform: bool,
526
527    /// When `true`, this is a privileged/server-managed field: the untrusted
528    /// JSON write path (`insert_json`/`update_json` — REST create/update and
529    /// admin form-submit) strips it UNLESS the caller explicitly authorizes it
530    /// via [`crate::orm::dynamic::DynQuerySet::allow_privileged`]. Set via
531    /// `#[umbral(privileged)]`.
532    ///
533    /// This is the default-DENY mass-assignment guard (audit_2 H3): fields like
534    /// `is_superuser` / `is_staff` / ownership FKs stay writable through the
535    /// typed struct path and through an *authorized* dynamic write, but an
536    /// unprivileged client can't set them by smuggling them into a create/update
537    /// body. Unlike `noform`, the field still renders on forms (an admin with
538    /// the right permission legitimately edits it); the guard is on the write,
539    /// not the visibility. OpenAPI is unaffected — the field remains in the
540    /// writable schema, since whether a given caller may set it is a runtime
541    /// authorization decision, not a static contract.
542    pub privileged: bool,
543
544    /// For `SqlType::ForeignKey` fields: whether the migration engine
545    /// emits a *physical* `FOREIGN KEY ... REFERENCES` constraint.
546    /// Toggles the physical FK constraint. Set via
547    /// `#[umbral(db_constraint = false)]`; defaults to `true` (today's
548    /// behaviour — emit the constraint).
549    ///
550    /// When `false`, the FK stays a *logical* relation: the column +
551    /// `fk_target` are unchanged, so joins, `select_related`, and the
552    /// app-level `check_fk_row_exists` pre-validation all keep working —
553    /// but no `REFERENCES` clause is rendered. This is the only way to
554    /// model an FK whose target lives on a *different* database (a real
555    /// DB constraint can't span databases). The boot-time guard in
556    /// `App::build` rejects a cross-database FK that has NOT opted out
557    /// via this flag (`BuildError::CrossDatabaseForeignKey`). Closes
558    /// gaps2 #22. Ignored for non-FK fields.
559    pub db_constraint: bool,
560
561    /// When `true`, the admin shows this field disabled on the edit
562    /// form. Pure UX hint — no effect on the REST API or the OpenAPI
563    /// spec; clients can still POST/PUT/PATCH the column normally.
564    /// Set via `#[umbral(noedit)]`.
565    ///
566    /// Use case: a value the user supplies once at signup (`email`,
567    /// `username`) but isn't supposed to change later through the
568    /// admin. The REST API may still accept updates — gate that
569    /// separately via `ResourceConfig::hide(...)` or a permission
570    /// class if you want hard enforcement. To block writes entirely,
571    /// use `noform` instead.
572    ///
573    /// Has no effect when `noform` is also set.
574    pub noedit: bool,
575
576    /// When `true`, this field is the display string for the
577    /// model — the admin uses it as the default label in
578    /// `list_display` when the developer hasn't specified one
579    /// explicitly. Set via `#[umbral(string)]` /
580    /// `#[umbral(string = true)]`. Only meaningful on `String`-typed
581    /// columns; on non-string columns the admin falls back to the PK.
582    pub is_string_repr: bool,
583
584    /// Soft length cap for display. The admin truncates the value at
585    /// this many characters when rendering it in `list_display` so a
586    /// long body doesn't blow out a column. `0` means no truncation.
587    /// Set via `#[umbral(max_length = N)]`.
588    pub max_length: u32,
589
590    /// Closed-set values for a choices column, in declaration order.
591    /// Populated by the `#[derive(Model)]` macro for fields tagged
592    /// `#[umbral(choices)]` by reading `<T as ChoiceField>::VALUES` at
593    /// derive time. Empty slice means "not a choices field" — every
594    /// non-choices column uses the empty default.
595    ///
596    /// The migration engine emits a Postgres `CHECK (col IN (...))`
597    /// constraint when this slice is non-empty; the admin renders a
598    /// `<select>` widget with these as the `<option>` values.
599    pub choices: &'static [&'static str],
600
601    /// Human-readable labels matching `choices` position-for-position.
602    /// Used by the admin to render the `<select>` widget's option text.
603    /// Empty when `choices` is empty.
604    pub choice_labels: &'static [&'static str],
605
606    /// SQL `DEFAULT` clause for this column. Set via
607    /// `#[umbral(default = "...")]` — accepts a string literal that
608    /// the DDL pass passes verbatim into `DEFAULT '<value>'`. Empty
609    /// string means no default. Carried through to the migration
610    /// engine, which emits the `DEFAULT` on both `CREATE TABLE` and
611    /// `ALTER TABLE ADD COLUMN`.
612    pub default: &'static str,
613
614    /// When `true`, this column is a [`MultiChoice<E>`] field: TEXT
615    /// storage holding a CSV of the variants of `E`. The `choices` and
616    /// `choice_labels` slices carry the same metadata as a single-valued
617    /// choices field — the admin uses `is_multichoice` to pick the
618    /// checkbox-chip widget over the `<select>` widget.
619    ///
620    /// [`MultiChoice<E>`]: crate::orm::MultiChoice
621    pub is_multichoice: bool,
622
623    /// When `true`, the migration engine emits a `UNIQUE` constraint
624    /// on this column at `CREATE TABLE` time. Set via
625    /// `#[umbral(unique)]`. Closes gap #65.
626    ///
627    /// Scope at v1: applies to *new* tables only. Toggling `unique`
628    /// on an existing column does not generate an automatic
629    /// `ALTER TABLE ADD CONSTRAINT` — SQLite cannot add a unique
630    /// constraint without rebuilding the table, and the M8 diff
631    /// engine only watches `ty` and `nullable`. Add or remove
632    /// uniqueness on a live table via a hand-written migration
633    /// until the diff engine grows constraint-level ops.
634    ///
635    /// Primary-key columns are already implicitly unique, so this
636    /// flag is a no-op on a PK field. Set it on every other column
637    /// that needs database-enforced uniqueness (`username`,
638    /// `email`, opaque tokens, slugs, etc.) so handler-level
639    /// pre-checks become unnecessary.
640    pub unique: bool,
641
642    /// Referential action emitted on `DELETE` of the FK target row.
643    /// Only meaningful when `ty == ForeignKey`; ignored for every
644    /// other column. Set via `#[umbral(on_delete = "...")]`. Closes
645    /// gap #68. Defaults to `NoAction` so existing migrations
646    /// don't change shape.
647    pub on_delete: FkAction,
648
649    /// Referential action emitted on `UPDATE` of the FK target row's
650    /// primary key. Same FK-only semantics as `on_delete`; almost
651    /// nobody touches this in practice (PKs rarely move) but the
652    /// symmetry matches `REFERENCES ... ON UPDATE ...` and the
653    /// `on_delete` / `on_update` pair. Set via
654    /// `#[umbral(on_update = "...")]`.
655    pub on_update: FkAction,
656
657    /// When `true`, the migration engine emits a single-column
658    /// `CREATE INDEX` statement alongside the `CREATE TABLE`. Set
659    /// via `#[umbral(index)]`. Closes BUG-4 in
660    /// `bugs/tests/testBugs.md`.
661    ///
662    /// Index name convention: `idx_<table>_<column>`. Apps that
663    /// need a custom name, a multi-column index, or a partial
664    /// index write the `CREATE INDEX` by hand in a follow-up
665    /// migration.
666    pub index: bool,
667
668    /// When `true`, the column gets populated with `Utc::now()` at
669    /// row-creation time *only*. Set via `#[umbral(auto_now_add)]`.
670    /// Closes BUG-5 in `bugs/tests/testBugs.md`.
671    ///
672    /// **Where this fires:** the dynamic write path
673    /// (`DynQuerySet::insert_json`, used by `umbral-rest` /
674    /// `umbral-admin`). The typed `Manager::create(instance)` path
675    /// is user-controlled — the caller passes whatever value they
676    /// chose at the struct-init site. v1 scope: the framework
677    /// auto-populates only when the body / form omits the field.
678    pub auto_now_add: bool,
679
680    /// When `true`, the column gets populated with `Utc::now()` on
681    /// every write (create AND update). Set via `#[umbral(auto_now)]`. Closes
682    /// BUG-5 in `bugs/tests/testBugs.md`.
683    ///
684    /// **Where this fires:** the dynamic write path
685    /// (`DynQuerySet::insert_json` and `update_json`, used by
686    /// `umbral-rest` / `umbral-admin`). The typed paths stay
687    /// user-controlled at v1. Body-supplied values are kept —
688    /// users can override `auto_now` columns on the dynamic
689    /// path, matching the lenient "fill if missing" shape of
690    /// `auto_now_add`. An "always override" shape lands as
691    /// a future v2 toggle if a real consumer asks.
692    pub auto_now: bool,
693
694    /// Human-readable column description (help text).
695    /// Set via `#[umbral(help = "...")]`. Flows
696    /// through to:
697    ///
698    /// - OpenAPI `description` on the property schema (closes
699    ///   playground-openapi-gaps item 5).
700    /// - Admin form field hint (the small line below the
701    ///   input).
702    /// - Doc-comment-style introspection for any future code
703    ///   generator.
704    ///
705    /// Empty string means "no description" — the OpenAPI
706    /// emitter and admin form skip the surrounding markup
707    /// when this is unset.
708    pub help: &'static str,
709
710    /// Presentation hint for form-rendering surfaces. Set via
711    /// `#[umbral(widget = "markdown" | "rte" | "textarea" | ...)]`;
712    /// `None` (the default) means "let the renderer pick by
713    /// `SqlType`". features.md #4.
714    ///
715    /// It is **metadata only** — the column's `SqlType`, DDL, and
716    /// stored value are unchanged. A `widget = "markdown"` field is
717    /// still `TEXT`; the widget only tells the admin (or any plugin
718    /// form) to render a markdown editor instead of a bare
719    /// `<textarea>`, and pairs with the `{{ value | markdown }}`
720    /// filter on the display side. Excluded from the migration diff
721    /// for the same reason `help` / `example` are: no DB effect.
722    ///
723    /// Renderers fall back to the `SqlType`-derived input for any
724    /// widget name they don't recognise, so an unknown widget is a
725    /// soft no-op rather than an error — third-party plugins can ship
726    /// new widget names without the core knowing them.
727    pub widget: Option<&'static str>,
728
729    /// Sample value rendered as OpenAPI `example` on the property
730    /// schema. Set via `#[umbral(example = "...")]`. Closes
731    /// playground-openapi-gaps item 6.
732    ///
733    /// Empty string means no example. Emitted as a JSON string in
734    /// the spec — clients that want typed examples can coerce on
735    /// their end. Pairs naturally with `help` to make a column's
736    /// purpose clear in Swagger UI.
737    pub example: &'static str,
738
739    /// Optional numeric lower bound. Set via `#[umbral(min = N)]`.
740    /// Closes IMP-3 from `bugs/tests/testBugs.md`. Flows to:
741    ///
742    /// - OpenAPI `minimum` on the property schema.
743    /// - REST plugin's dynamic write path pre-validation (400
744    ///   response with a structured message).
745    /// - Future: HTML5 `min` attribute on admin form inputs.
746    ///
747    /// `i64::MIN` sentinel means "no minimum"; the DDL +
748    /// OpenAPI emitters skip the constraint when this is the
749    /// sentinel value. Macro accepts integer literals only at
750    /// v1 (a `Decimal`-aware shape can land when there's a real
751    /// consumer for decimal-typed validators).
752    pub min: Option<i64>,
753
754    /// Optional numeric upper bound. Set via `#[umbral(max = N)]`.
755    /// Mirror of `min`; same plumbing on the OpenAPI / REST /
756    /// admin sides.
757    pub max: Option<i64>,
758
759    /// Constrained-text marker. `None` is a plain `String` /
760    /// `SqlType::Text` column; `Some("slug" | "email" | "url")` is
761    /// one of the validator wrapper types from
762    /// [`crate::orm::validators`]. Closes BUG-11/12/13. Flows to:
763    ///
764    /// - OpenAPI `format: email` / `format: uri` / `pattern` on the
765    ///   property schema (the standard 3.0 markers).
766    /// - REST plugin's dynamic write path: `validate_text_format`
767    ///   pre-checks the body value and returns a structured 400
768    ///   on a bad input.
769    /// - Admin form: HTML5 `type="email"` / `type="url"` widget
770    ///   (when those land).
771    ///
772    /// The marker is set by the macro classifier from the field type
773    /// — `Slug` → `Some("slug")`, `Email` → `Some("email")`,
774    /// `Url` → `Some("url")`. The wrapper type + marker stay in sync
775    /// because they're produced from the same single match arm in
776    /// `umbral-macros::classify_field_type`.
777    pub text_format: Option<&'static str>,
778
779    /// Source column for an auto-derived slug. Set via
780    /// `#[umbral(slug_from = "title")]` on a `Slug` / `String` field;
781    /// names a sibling column on the same model whose value seeds
782    /// this column at write time. Gap 109.
783    ///
784    /// **Where this fires:** the dynamic write path
785    /// ([`crate::orm::DynQuerySet::insert_json`] +
786    /// [`crate::orm::DynQuerySet::update_json`]). On insert, an empty
787    /// or absent slug column is replaced by `slugify(source_value)`
788    /// derived from the source column in the same body. On update,
789    /// the slug is regenerated only when the source column is also
790    /// in the update payload, so callers who edit nothing but the
791    /// slug itself keep their hand-tuned value.
792    ///
793    /// `None` is the default — no auto-derive. The string is a
794    /// column name (snake_case), not a Rust field name, so it must
795    /// match exactly what ends up in `FieldSpec::name`.
796    pub slug_from: Option<&'static str>,
797}
798
799/// Referential action emitted in the SQL `REFERENCES ... ON
800/// {DELETE,UPDATE} <action>` clause. Mirrors the standard SQL set.
801///
802/// Copy + 'static so it can live on `FieldSpec` (which is itself
803/// `Copy` for storage in `&'static [FieldSpec]`).
804#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
805pub enum FkAction {
806    /// SQL `NO ACTION` — the default. The migration engine emits no
807    /// clause at all (which means "default" on both backends; sqlite
808    /// and Postgres both default to NO ACTION when omitted).
809    #[default]
810    NoAction,
811    /// SQL `CASCADE` — when the FK target row is deleted/updated,
812    /// the referencing row is deleted/updated too. The right answer
813    /// for "owned" relationships (an `AuthToken` follows its
814    /// owning `AuthUser` to the grave).
815    Cascade,
816    /// SQL `RESTRICT` — block the delete/update of the FK target
817    /// row if any referencing row exists. Checked immediately;
818    /// doesn't defer to commit. Right for "you can't drop a
819    /// category that still has products in it."
820    Restrict,
821    /// SQL `SET NULL` — null the referencing column. Only valid on
822    /// nullable FK columns; the migration engine doesn't currently
823    /// check this at boot, so a mismatched pair (NOT NULL + SET NULL)
824    /// will fail at FK action time, not at CREATE TABLE.
825    SetNull,
826}
827
828impl FkAction {
829    /// SQL keyword for the `ON {DELETE,UPDATE} <kw>` clause.
830    /// Returns `None` for `NoAction` so the DDL builder can skip
831    /// the clause entirely (rather than emitting the redundant
832    /// `NO ACTION` literal).
833    pub fn sql_keyword(self) -> Option<&'static str> {
834        match self {
835            Self::NoAction => None,
836            Self::Cascade => Some("CASCADE"),
837            Self::Restrict => Some("RESTRICT"),
838            Self::SetNull => Some("SET NULL"),
839        }
840    }
841
842    /// Parse the attribute string supplied to `#[umbral(on_delete = "...")]`.
843    /// Case-insensitive; accepts both `set_null` and `set null` for
844    /// the multi-word case so users can write whichever feels
845    /// natural.
846    pub fn from_attr_str(s: &str) -> Option<Self> {
847        match s.to_lowercase().as_str() {
848            "no_action" | "no action" => Some(Self::NoAction),
849            "cascade" => Some(Self::Cascade),
850            "restrict" => Some(Self::Restrict),
851            "set_null" | "set null" => Some(Self::SetNull),
852            _ => None,
853        }
854    }
855}
856
857/// The SQL type kind of a column.
858///
859/// The dialect-specific rendering (`BIGINT` vs `INTEGER` vs whatever
860/// the backend calls it) is the backend's responsibility, set up by
861/// the M4 `DatabaseBackend` abstraction. This enum is the abstract
862/// classification umbral reasons about.
863///
864/// The catalogue follows spec 04 §4.1: each variant covers one
865/// field type. Rust types in the field declaration map to a
866/// variant via the M3 derive's `classify_field_type`; the table is in
867/// `umbral-macros/src/lib.rs` alongside the derive.
868///
869/// Backend-specific variants (Postgres `Array`, `HStore`, `Jsonb`) land
870/// at M4 when the system check exists to gate them at boot.
871#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
872pub enum SqlType {
873    /// A foreign-key reference to another table. Stored as `i64` (the
874    /// referenced row's primary key). Renders as `BIGINT REFERENCES
875    /// "<target_table>"("id")` on both Postgres and SQLite.
876    ///
877    /// The referenced table name is carried separately in
878    /// [`FieldSpec::fk_target`] so this enum stays `Copy`. The migration
879    /// engine reads `fk_target` at DDL-emit time.
880    ///
881    /// Out of scope at v1: non-`i64` FK targets, `ON DELETE` behaviours
882    /// beyond the default RESTRICT, reverse accessors (`User::posts`),
883    /// and many-to-many join tables. See `docs/specs/relationships.md`.
884    ForeignKey,
885    /// 16-bit signed integer. `i8` / `i16` / `u8` in Rust.
886    SmallInt,
887    /// 32-bit signed integer. `i32` / `u16` in Rust.
888    Integer,
889    /// 64-bit signed integer. `i64` / `u32` in Rust.
890    BigInt,
891    /// 32-bit floating point. `f32` in Rust.
892    Real,
893    /// 64-bit floating point. `f64` in Rust.
894    Double,
895    /// Boolean. `bool` in Rust.
896    Boolean,
897    /// Variable-length string. `String` in Rust.
898    Text,
899    /// Date without time. `chrono::NaiveDate` in Rust.
900    Date,
901    /// Time without date. `chrono::NaiveTime` in Rust.
902    Time,
903    /// Timestamp with timezone. `chrono::DateTime<chrono::Utc>` in Rust.
904    Timestamptz,
905    /// 128-bit UUID. `uuid::Uuid` in Rust.
906    Uuid,
907    /// JSON document. `serde_json::Value` in Rust.
908    ///
909    /// Cross-backend: Postgres stores native `JSONB` (binary form with
910    /// index / operator support); SQLite stores `TEXT` (JSON-as-string).
911    /// `serde_json::Value` round-trips through both via sqlx's `json`
912    /// feature, so a user model with a `Value` field works on either
913    /// backend without code changes: a portable JSON field with a
914    /// portable shape and dialect-specific storage. Native JSONB-only
915    /// operators (`@>`, `->`, `->>` etc.) are a deferred follow-on
916    /// landed alongside Postgres-specific column predicates.
917    Json,
918    /// Array column. `Vec<T>` in Rust where `T` is one of the
919    /// [`ArrayElement`] variants.
920    ///
921    /// **Postgres-only.** SQLite has no native array type; the M4
922    /// system check fails at boot if an Array field is registered
923    /// against the SQLite backend. For portable list storage, declare
924    /// the field as `serde_json::Value` (the [`Self::Json`] variant)
925    /// and store a JSON array inside.
926    ///
927    /// The inner type is restricted to [`ArrayElement`] rather than
928    /// `Box<SqlType>` so the outer enum stays `Copy` and `SqlType`
929    /// values can live in `const FIELDS` slices the derive emits.
930    /// Multi-dim arrays (`Vec<Vec<T>>`), nullable elements
931    /// (`Vec<Option<T>>`), and nested JSON arrays (`Vec<Value>`) are
932    /// out of scope for v1.
933    Array(ArrayElement),
934    /// `INET` — Postgres IP address column with optional netmask.
935    /// Maps to `ipnetwork::IpNetwork` in Rust. **Postgres-only.**
936    /// Stores a generic IP address.
937    Inet,
938    /// `CIDR` — Postgres network address column. Same Rust type as
939    /// `Inet` (`ipnetwork::IpNetwork`) but with the constraint that
940    /// the host bits must be zero. **Postgres-only.**
941    Cidr,
942    /// `MACADDR` — Postgres MAC address column. Maps to
943    /// `mac_address::MacAddress` in Rust. **Postgres-only.**
944    MacAddr,
945    /// `XML` — Postgres XML document column. Maps to `String` in Rust
946    /// (umbral stores and round-trips the serialized XML text; it does
947    /// not parse or validate the document at the framework level —
948    /// Postgres does that on insert). **Postgres-only.** Reach for this
949    /// over `Text` only when you want Postgres' `xml` type checking and
950    /// the `xpath` / `xmlexists` operator surface; otherwise `Text`
951    /// stores XML strings just fine (XML is otherwise modelled as plain
952    /// text).
953    Xml,
954    /// `LTREE` — Postgres hierarchical label-path column (the `ltree`
955    /// extension). Maps to `String` in Rust (the dotted path, e.g.
956    /// `"Top.Science.Astronomy"`). **Postgres-only**, and requires the
957    /// `ltree` extension (`CREATE EXTENSION ltree`) to be installed in
958    /// the target database. The umbral migration engine emits the bare
959    /// `ltree` column type; the extension itself is the operator's
960    /// responsibility (a hand-written migration or a DB bootstrap step).
961    Ltree,
962    /// `BIT VARYING` — Postgres bit-string column. Maps to `String` in
963    /// Rust (the textual `"0"`/`"1"` representation, e.g. `"101"`).
964    /// **Postgres-only.** v1 renders as `BIT VARYING` (variable-length);
965    /// a fixed-width `BIT(n)` needs a hand-written migration after the
966    /// initial create until a `#[umbral(bit_len = N)]` attribute lands
967    /// for a real consumer. There is otherwise no dedicated bit-string
968    /// type; the fallback is plain text.
969    Bit,
970    /// `TSVECTOR` — Postgres full-text search lexeme vector. Maps to
971    /// [`crate::orm::TsVector`] in Rust (a thin newtype around
972    /// `String` with sqlx Type/Encode/Decode impls). **Postgres-only.**
973    ///
974    /// The column is typically populated by a Postgres trigger or
975    /// `GENERATED ALWAYS AS (to_tsvector(...)) STORED` clause; umbral's
976    /// migration engine emits the bare `tsvector` type, leaving the
977    /// population mechanism to the user. Queries against a
978    /// `FullTextCol` use the `@@` match operator with `to_tsquery` /
979    /// `websearch_to_tsquery`.
980    FullText,
981    /// `BLOB` (SQLite) / `BYTEA` (Postgres) — arbitrary binary payload.
982    /// Maps to `Vec<u8>` in Rust. Used by anything that stores opaque
983    /// bytes: file uploads, the cache backend's value column, encrypted
984    /// envelopes, etc.
985    ///
986    /// `Vec<u8>` was previously routed to `SqlType::Array(SmallInt)`
987    /// because the array detection treated `u8` as a small int. The
988    /// detection now checks for `Vec<u8>` specifically first and
989    /// routes to `Bytes`; `Vec<i8>` / `Vec<i16>` still map to
990    /// `Array(SmallInt)`.
991    Bytes,
992    /// `NUMERIC(19, 4)` — fixed-point decimal. Maps to
993    /// `rust_decimal::Decimal` in Rust. Closes BUG-10 from
994    /// `bugs/tests/testBugs.md`. Money / price columns must use
995    /// this, not `f64` (binary float drops cents) or `String`
996    /// (no DB-level arithmetic).
997    ///
998    /// **Postgres-only at v1.** sqlx's `rust_decimal` feature
999    /// adds Encode/Decode for Postgres `NUMERIC` only; SQLite has
1000    /// no native decimal type (every numeric value is INTEGER /
1001    /// REAL / TEXT affinity). The boot system check rejects
1002    /// Decimal models against SQLite the same way it rejects
1003    /// `Array(_)` — apps deploying to SQLite either pick a
1004    /// portable type (`Real` or `Text` with manual formatting) or
1005    /// use Postgres for the parts of their schema that need
1006    /// decimal arithmetic. A fixed-precision decimal column.
1007    ///
1008    /// **v1 scope.** Precision and scale are fixed at `(19, 4)` —
1009    /// 19 significant digits, 4 after the decimal point. That's
1010    /// enough headroom for currency values up to one quadrillion
1011    /// dollars (with sub-cent precision) and matches sqlx's
1012    /// `Decimal` default. Apps that need a different precision
1013    /// alter the column via a hand-written migration after the
1014    /// initial create. A `#[umbral(precision = N, scale = M)]`
1015    /// attribute lands when there's a real consumer that needs
1016    /// dimensions outside the default.
1017    Decimal,
1018}
1019
1020/// Element types valid inside [`SqlType::Array`].
1021///
1022/// A strict subset of the [`SqlType`] catalogue: the value types
1023/// Postgres supports as `T[]` and that umbral knows how to bind / decode
1024/// through sqlx. Stays `Copy` so the outer `SqlType::Array(ArrayElement)`
1025/// remains usable in `const FIELDS` slices.
1026///
1027/// Catalogue:
1028///
1029/// | Variant     | Postgres type | Rust inner type   |
1030/// |-------------|---------------|-------------------|
1031/// | `SmallInt`  | `int2[]`      | `Vec<i16>`        |
1032/// | `Integer`   | `int4[]`      | `Vec<i32>`        |
1033/// | `BigInt`    | `int8[]`      | `Vec<i64>`        |
1034/// | `Real`      | `float4[]`    | `Vec<f32>`        |
1035/// | `Double`    | `float8[]`    | `Vec<f64>`        |
1036/// | `Boolean`   | `bool[]`      | `Vec<bool>`       |
1037/// | `Text`      | `text[]`      | `Vec<String>`     |
1038/// | `Uuid`      | `uuid[]`      | `Vec<uuid::Uuid>` |
1039///
1040/// Other element types (Date / Time / Timestamptz / Json) land as
1041/// follow-ons when there's a real consumer; the binding semantics for
1042/// chrono types as Postgres array elements need a deliberate pass.
1043#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
1044pub enum ArrayElement {
1045    SmallInt,
1046    Integer,
1047    BigInt,
1048    Real,
1049    Double,
1050    Boolean,
1051    Text,
1052    Uuid,
1053}
1054
1055impl ArrayElement {
1056    /// Lift this element type back to its [`SqlType`] equivalent. Used
1057    /// when a per-element decision needs to dispatch through the same
1058    /// SqlType match the rest of umbral uses (e.g. picking a
1059    /// `sea_query::ColumnType` for the element).
1060    pub fn to_sql_type(self) -> SqlType {
1061        match self {
1062            ArrayElement::SmallInt => SqlType::SmallInt,
1063            ArrayElement::Integer => SqlType::Integer,
1064            ArrayElement::BigInt => SqlType::BigInt,
1065            ArrayElement::Real => SqlType::Real,
1066            ArrayElement::Double => SqlType::Double,
1067            ArrayElement::Boolean => SqlType::Boolean,
1068            ArrayElement::Text => SqlType::Text,
1069            ArrayElement::Uuid => SqlType::Uuid,
1070        }
1071    }
1072}