Struct sea_query::foreign_key::ForeignKeyCreateStatement[][src]

pub struct ForeignKeyCreateStatement { /* fields omitted */ }
Expand description

Create a foreign key constraint for an existing table. Unsupported by Sqlite

Examples

use sea_query::{*, tests_cfg::*};

let foreign_key = ForeignKey::create()
    .name("FK_character_font")
    .from(Char::Table, Char::FontId)
    .to(Font::Table, Font::Id)
    .on_delete(ForeignKeyAction::Cascade)
    .on_update(ForeignKeyAction::Cascade)
    .to_owned();

assert_eq!(
    foreign_key.to_string(MysqlQueryBuilder),
    vec![
        r#"ALTER TABLE `character`"#,
        r#"ADD CONSTRAINT `FK_character_font`"#,
        r#"FOREIGN KEY (`font_id`) REFERENCES `font` (`id`)"#,
        r#"ON DELETE CASCADE ON UPDATE CASCADE"#,
    ].join(" ")
);
assert_eq!(
    foreign_key.to_string(PostgresQueryBuilder),
    vec![
        r#"ALTER TABLE "character" ADD CONSTRAINT "FK_character_font""#,
        r#"FOREIGN KEY ("font_id") REFERENCES "font" ("id")"#,
        r#"ON DELETE CASCADE ON UPDATE CASCADE"#,
    ].join(" ")
);

Composite key

use sea_query::{*, tests_cfg::*};

let foreign_key = ForeignKey::create()
    .name("FK_character_glyph")
    .from(Char::Table, (Char::FontId, Char::Id))
    .to(Glyph::Table, (Char::FontId, Glyph::Id))
    .on_delete(ForeignKeyAction::Cascade)
    .on_update(ForeignKeyAction::Cascade)
    .to_owned();

assert_eq!(
    foreign_key.to_string(MysqlQueryBuilder),
    vec![
        r#"ALTER TABLE `character`"#,
        r#"ADD CONSTRAINT `FK_character_glyph`"#,
        r#"FOREIGN KEY (`font_id`, `id`) REFERENCES `glyph` (`font_id`, `id`)"#,
        r#"ON DELETE CASCADE ON UPDATE CASCADE"#,
    ].join(" ")
);
assert_eq!(
    foreign_key.to_string(PostgresQueryBuilder),
    vec![
        r#"ALTER TABLE "character" ADD CONSTRAINT "FK_character_glyph""#,
        r#"FOREIGN KEY ("font_id", "id") REFERENCES "glyph" ("font_id", "id")"#,
        r#"ON DELETE CASCADE ON UPDATE CASCADE"#,
    ].join(" ")
);

Implementations

impl ForeignKeyCreateStatement[src]

pub fn new() -> Self[src]

Construct a new ForeignKeyCreateStatement

pub fn name(self, name: &str) -> Self[src]

Set foreign key name

pub fn table<T: 'static, R: 'static>(self, table: T, ref_table: R) -> Self where
    T: Iden,
    R: Iden
[src]

👎 Deprecated since 0.10.2:

Please use the [ForeignKeyCreateStatement::from] and [ForeignKeyCreateStatement::to]

Set key table and referencing table

pub fn col<T: 'static, R: 'static>(self, column: T, ref_column: R) -> Self where
    T: Iden,
    R: Iden
[src]

👎 Deprecated since 0.10.2:

Please use the [ForeignKeyCreateStatement::from] and [ForeignKeyCreateStatement::to]

Set key column and referencing column

pub fn from<T, C>(self, table: T, columns: C) -> Self where
    T: IntoIden,
    C: IdenList
[src]

Set key table and columns

pub fn to<T, C>(self, table: T, columns: C) -> Self where
    T: IntoIden,
    C: IdenList
[src]

Set referencing table and columns

pub fn from_tbl<T>(self, table: T) -> Self where
    T: IntoIden
[src]

Set key table

pub fn to_tbl<R>(self, ref_table: R) -> Self where
    R: IntoIden
[src]

Set referencing table

pub fn from_col<T>(self, column: T) -> Self where
    T: IntoIden
[src]

Add key column

pub fn to_col<R>(self, ref_column: R) -> Self where
    R: IntoIden
[src]

Add referencing column

pub fn on_delete(self, action: ForeignKeyAction) -> Self[src]

Set on delete action

pub fn on_update(self, action: ForeignKeyAction) -> Self[src]

Set on update action

impl ForeignKeyCreateStatement[src]

pub fn to_string<T: SchemaBuilder>(&self, schema_builder: T) -> String[src]

pub fn build<T: SchemaBuilder>(&self, schema_builder: T) -> String[src]

pub fn build_any(&self, schema_builder: &dyn SchemaBuilder) -> String[src]

Trait Implementations

impl Clone for ForeignKeyCreateStatement[src]

fn clone(&self) -> ForeignKeyCreateStatement[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl Debug for ForeignKeyCreateStatement[src]

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

Formats the value using the given formatter. Read more

impl Default for ForeignKeyCreateStatement[src]

fn default() -> Self[src]

Returns the “default value” for a type. Read more

impl SchemaStatementBuilder for ForeignKeyCreateStatement[src]

fn build<T: SchemaBuilder>(&self, schema_builder: T) -> String[src]

Build corresponding SQL statement for certain database backend and return SQL string

fn build_any(&self, schema_builder: &dyn SchemaBuilder) -> String[src]

Build corresponding SQL statement for certain database backend and return SQL string

fn to_string<T: SchemaBuilder>(&self, schema_builder: T) -> String[src]

Build corresponding SQL statement for certain database backend and return SQL string

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

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

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>, 

pub fn vzip(self) -> V