Skip to main content

InsertBuilder

Struct InsertBuilder 

Source
pub struct InsertBuilder<'a, M: Model> { /* private fields */ }
Expand description

INSERT query builder.

§Example

// Simple insert
let id = insert!(hero).execute(cx, &conn).await?;

// Insert with RETURNING
let row = insert!(hero).returning().execute_returning(cx, &conn).await?;

// Insert with UPSERT
let id = insert!(hero)
    .on_conflict_do_nothing()
    .execute(cx, &conn).await?;

Implementations§

Source§

impl<'a, M: Model> InsertBuilder<'a, M>

Source

pub fn new(model: &'a M) -> Self

Create a new INSERT builder for the given model instance.

Source

pub fn returning(self) -> Self

Add RETURNING * clause to return the inserted row.

Use with execute_returning() to get the inserted row.

Source

pub fn on_conflict_do_nothing(self) -> Self

Handle conflicts by doing nothing (PostgreSQL ON CONFLICT DO NOTHING).

This allows the insert to silently succeed even if it would violate a unique constraint.

Source

pub fn on_conflict_do_update(self, columns: &[&str]) -> Self

Handle conflicts by updating specified columns (UPSERT).

If columns is empty, all non-primary-key columns are updated. The conflict target defaults to the primary key.

§Example
// Update name and age on conflict
insert!(hero)
    .on_conflict_do_update(&["name", "age"])
    .execute(cx, &conn).await?;
Source

pub fn on_conflict_target_do_update( self, target: &[&str], columns: &[&str], ) -> Self

Handle conflicts by updating columns with a specific conflict target.

§Arguments
  • target - The columns that form the unique constraint to match
  • columns - The columns to update on conflict
Source

pub fn build(&self) -> (String, Vec<Value>)

Build the INSERT SQL and parameters with default dialect (Postgres).

Source

pub fn build_with_dialect(&self, dialect: Dialect) -> (String, Vec<Value>)

Build the INSERT SQL and parameters with specific dialect.

Source

pub async fn execute<C: Connection>( self, cx: &Cx, conn: &C, ) -> Outcome<i64, Error>

Execute the INSERT and return the inserted ID.

Source

pub async fn execute_returning<C: Connection>( self, cx: &Cx, conn: &C, ) -> Outcome<Option<Row>, Error>

Execute the INSERT with RETURNING and get the inserted row.

This automatically adds RETURNING * and returns the full row.

Trait Implementations§

Source§

impl<'a, M: Debug + Model> Debug for InsertBuilder<'a, M>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, M> Freeze for InsertBuilder<'a, M>

§

impl<'a, M> RefUnwindSafe for InsertBuilder<'a, M>
where M: RefUnwindSafe,

§

impl<'a, M> Send for InsertBuilder<'a, M>

§

impl<'a, M> Sync for InsertBuilder<'a, M>

§

impl<'a, M> Unpin for InsertBuilder<'a, M>

§

impl<'a, M> UnwindSafe for InsertBuilder<'a, M>
where M: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, _span: NoopSpan) -> Self

Instruments this future with a span (no-op when disabled).
Source§

fn in_current_span(self) -> Self

Instruments this future with the current span (no-op when disabled).
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more