Skip to main content

Blueprint

Struct Blueprint 

Source
pub struct Blueprint { /* private fields */ }
Expand description

A table being created.

Implementations§

Source§

impl Blueprint

Source

pub fn new(table: impl Into<String>) -> Blueprint

A blueprint for table.

Source

pub fn create( table: impl Into<String>, build: impl FnOnce(&mut Blueprint), ) -> Blueprint

Describe table with build, and hand back the blueprint.

Source

pub fn table(&self) -> &str

The table’s name.

Source

pub fn strict(&mut self) -> &mut Blueprint

Emit a plain CREATE TABLE, so creating one that exists is an error rather than a no-op.

The default is IF NOT EXISTS, which makes a migration safe to re-run by hand against a database somebody has already touched.

Source

pub fn column( &mut self, name: impl Into<String>, kind: ColumnKind, ) -> &mut Column

A column of any kind. The typed helpers below all come through here.

Source

pub fn id(&mut self) -> &mut Column

An auto-incrementing primary key named id.

Source

pub fn big_increments(&mut self, name: impl Into<String>) -> &mut Column

An auto-incrementing 64-bit primary key.

Source

pub fn increments(&mut self, name: impl Into<String>) -> &mut Column

An auto-incrementing 32-bit primary key.

Source

pub fn string(&mut self, name: impl Into<String>) -> &mut Column

VARCHAR(255).

Source

pub fn string_len(&mut self, name: impl Into<String>, len: u32) -> &mut Column

VARCHAR(len).

Source

pub fn text(&mut self, name: impl Into<String>) -> &mut Column

Unbounded text.

Not indexable on MySQL without a prefix length, so reach for string for anything you will search or sort by.

Source

pub fn integer(&mut self, name: impl Into<String>) -> &mut Column

A 32-bit signed integer.

Source

pub fn big_integer(&mut self, name: impl Into<String>) -> &mut Column

A 64-bit signed integer.

Source

pub fn unsigned_integer(&mut self, name: impl Into<String>) -> &mut Column

A 32-bit unsigned integer.

Source

pub fn unsigned_big_integer(&mut self, name: impl Into<String>) -> &mut Column

A 64-bit unsigned integer.

Source

pub fn foreign_id(&mut self, name: impl Into<String>) -> &mut Column

A foreign key column: unsigned 64-bit and indexed, ready for constrained_on.

ⓘ
table.foreign_id("author_id").constrained_on("users").cascade_on_delete();
Source

pub fn boolean(&mut self, name: impl Into<String>) -> &mut Column

A boolean. An integer on SQLite and MySQL, which have no boolean type.

Source

pub fn double(&mut self, name: impl Into<String>) -> &mut Column

A double-precision float. Not for money — see decimal.

Source

pub fn decimal( &mut self, name: impl Into<String>, precision: u32, scale: u32, ) -> &mut Column

A fixed-point number, for money and anything else that must not round.

Read ColumnKind::Decimal before using it on SQLite, which has no fixed-point type and will store a float.

Source

pub fn timestamp(&mut self, name: impl Into<String>) -> &mut Column

A timestamp.

Source

pub fn date(&mut self, name: impl Into<String>) -> &mut Column

A calendar date, with no time.

Source

pub fn binary(&mut self, name: impl Into<String>) -> &mut Column

Raw bytes.

Source

pub fn json(&mut self, name: impl Into<String>) -> &mut Column

A JSON document.

Source

pub fn timestamps(&mut self) -> &mut Blueprint

created_at and updated_at, both nullable.

Nullable because nothing here writes them for you — a row inserted by a migration or by hand would otherwise fail. A model that always sets them can declare its own NOT NULL columns instead.

Source

pub fn soft_deletes(&mut self) -> &mut Column

A nullable deleted_at, for soft deletes.

Source

pub fn primary<I, S>(&mut self, columns: I) -> &mut Blueprint
where I: IntoIterator<Item = S>, S: Into<String>,

A composite primary key.

What a pivot table wants: the pair is the key, so the same link cannot be inserted twice.

Source

pub fn index<I, S>(&mut self, columns: I) -> &mut IndexDef
where I: IntoIterator<Item = S>, S: Into<String>,

A secondary index, named by convention.

Source

pub fn unique<I, S>(&mut self, columns: I) -> &mut IndexDef
where I: IntoIterator<Item = S>, S: Into<String>,

A UNIQUE index over one or more columns.

Source

pub fn to_statement(&self) -> TableCreateStatement

The CREATE TABLE statement, before rendering.

Source

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

Every statement that creates this table on dialect, in order: the table, then its indexes.

Source

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

The statements that undo it: drop the table.

The indexes go with it — no engine keeps an index on a table that is no longer there — so one statement is the whole reversal.

Trait Implementations§

Source§

impl Clone for Blueprint

Source§

fn clone(&self) -> Blueprint

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Blueprint

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Event for T
where T: Send + Sync + 'static,

Source§

fn event_name() -> &'static str
where Self: Sized,

A human-readable name, used by wildcard listeners and log output. Always the Rust type path; see the trait docs.
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: 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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