Skip to main content

Module insert

Module insert 

Source
Expand description

INSERT INTO .., from values or from a query.

A column with a schema default gets a Defaultable<T> field, so “omit” and “explicit value” stay distinguishable — and Defaultable<Option<T>> when it’s also nullable, making that three distinct states. Omission renders as the DEFAULT keyword in that row’s VALUES (..) tuple rather than changing the column list, so rows that omit different fields still share one statement.

An ON CONFLICT target names columns, and the database infers an index from them — one over exactly those columns whose own predicate the target’s implies. A partial unique index therefore needs its predicate repeated, which is what partial_index(..) is for.

Structs§

ConflictUpdate
The DO UPDATE half of an upsert: everything an UPDATE assigns, plus excluded, plus the WHERE that decides whether the update fires at all. An Assignments converts into one, so a request-shaped patch reaches an upsert as it always did; the reverse doesn’t exist, which is what keeps a list naming the proposed row out of a statement that has none.
Excluded
The row an INSERT proposed, as ON CONFLICT DO UPDATE sees it: a pseudo-table holding the target’s own columns. Deliberately not a BaseTable, so it is not a table a query can select from — excluded is the only way to name a column of it.
Insert
InsertSeed
InsertSelect
INSERT INTO t (..) SELECT .. — rows a query produces rather than rows a caller holds. From InsertSeed::select.
Missing
A column an *Insert builder hasn’t been given a value for yet. Named after the column so the builder’s type says which one is missing, rather than leaving a bare () to be counted by position.
NothingToInsert
An INSERT was given no rows at all. Returned rather than panicked for the reason update::NothingToSet gives: an empty collection is ordinary request-shaped data, and the caller decides whether it is a no-op or an error.
PartialIndex
A conflict target narrowed to a partial unique index, from partial_index. It takes the columns rather than another target, so the predicate it carries is the only one there is.

Enums§

Defaultable
InsertValue

Traits§

ConflictColumns
The columns an ON CONFLICT target infers an index from: one or more, proven by T to belong to the table being inserted into, where a raw &[&str] would let a typo through to the database. Implemented for a bare Column<C> and for tuples of up to three; add arities as real schemas need them.
ConflictTarget
An ON CONFLICT target: the columns, and for a partial unique index the predicate that picks it. Implemented for everything ConflictColumns is, plus the partial_index those columns pass through.
Filled
Proof that a builder’s slot for column C holds that column’s value. Deliberately unsealed, unlike scope::Find: forging it buys nothing, because *Insert’s fields are public and a complete row with a value of the caller’s choosing is directly constructible. What the type-state builder prevents is forgetting a column, not choosing its value — and a seal here can’t hold anyway, since the derive must implement this in the schema’s own crate, where any nameable proof is nameable twice. Missing<C> doesn’t implement it, which is what build() is bounded by — on the method rather than by the slot’s type, so an incomplete row is a sentence naming the column rather than a missing build.
InsertRow
Implemented by the #[derive(Table)]-generated *Insert struct for each table. One list of (column, value) pairs rather than a name list beside a value list, for the reason select::AllColumns carries one list: the seal is #[doc(hidden)] pub — the derive has to write it in the schema’s own crate — so two lists that have to line up position for position could be made not to, and INSERT INTO t (a, b, c) VALUES ($1) is malformed whatever the table looks like. update::UpdateRow::sets has always had this shape.
InsertValues
A chain of (column, value) cells: RowCons<C, InsertValue, Tail> down to RowNil. Walked once for the header and once for each row’s values, so the two cannot disagree.
Insertable
A table with at least one column a statement may insert into. Emitted by #[derive(Table)] unless every column is generated, which leaves an INSERT with nothing to name: SQL spells that DEFAULT VALUES, and spells it for exactly one row.
IntoColumnValue
What a column’s setter accepts, keyed by what that column’s field holds: the column’s own Rust type — &str for text — plus the Option a request struct already carries, wherever leaving the column out means something. What None means is the position’s own: on an insert it is NULL for a nullable column and the schema’s default for a defaulted one (.<column>_null() says the other, where a column is both); on an update it is untouched, since an UPDATE that says nothing about a column leaves it alone.

Functions§

excluded
excluded.column — the value the column would have taken had the row inserted, which is what SET total = total + EXCLUDED.total and the plain SET v = EXCLUDED.v of every upsert are written with. Reads as an ordinary expression over the target’s columns, so it composes with them through everything that takes one; what confines it is the scope it is discharged against, and ConflictUpdate is the only list with it.
insert
partial_index
ON CONFLICT (a, b) WHERE deleted_at IS NULL — the conflict target of a partial unique index.

Type Aliases§

ConflictScope
What a DO UPDATE assignment may name: the conflicting row and the proposed one. An UPDATE’s own SET list is discharged against WrittenTable alone, which is what keeps excluded out of a statement that has no proposed row.