pub trait Insertable<T> {
type Values;
// Required method
fn values(self) -> Self::Values;
// Provided method
fn insert_into(self, table: T) -> InsertStatement<T, Self::Values>
where Self: Sized { ... }
}Expand description
Represents that a structure can be used to insert a new row into the
database. This is automatically implemented for &[T] and &Vec<T> for
inserting more than one record.
§Deriving
This trait can be automatically derived by adding #[derive(Insertable)]
to your struct. Structs which derive this trait must also be annotated
with #[table_name = "some_table_name"]. If the field name of your
struct differs from the name of the column, you can annotate the field
with #[column_name = "some_column_name"].
Your struct can also contain fields which implement Insertable. This is
useful when you want to have one field map to more than one column (for
example, an enum that maps to a label and a value column). Add
#[diesel(embed)] to any such fields.
Required Associated Types§
Required Methods§
Provided Methods§
Sourcefn insert_into(self, table: T) -> InsertStatement<T, Self::Values>where
Self: Sized,
fn insert_into(self, table: T) -> InsertStatement<T, Self::Values>where
Self: Sized,
Insert self into a given table.
foo.insert_into(table) is identical to insert_into(table).values(foo).
However, when inserting from a select statement,
this form is generally preferred.
§Example
users::table
.select((
users::name.concat("'s First Post"),
users::id,
))
.insert_into(posts::table)
.into_columns((posts::title, posts::user_id))
.execute(&conn)?;
let inserted_posts = posts::table
.select(posts::title)
.load::<String>(&conn)?;
let expected = vec!["Sean's First Post", "Tess's First Post"];
assert_eq!(expected, inserted_posts);