Function diesel::insert [] [src]

pub fn insert<T: ?Sized>(records: &T) -> IncompleteInsertStatement<&T, Insert>

Creates an insert statement. Will add the given data to a table. This function is not exported by default. As with other commands, the resulting query can return the inserted rows if you choose.

Examples

// Insert one record at a time

let new_user = NewUser { name: "Ruby Rhod".to_string() };

diesel::insert(&new_user)
    .into(users)
    .execute(&connection)
    .unwrap();

// Insert many records

let new_users = vec![
    NewUser { name: "Leeloo Multipass".to_string(), },
    NewUser { name: "Korben Dallas".to_string(), },
];

let inserted_names = diesel::insert(&new_users)
    .into(users)
    .execute(&connection)
    .unwrap();

With return value

// postgres only
let new_users = vec![
    NewUser { name: "Diva Plavalaguna".to_string(), },
    NewUser { name: "Father Vito Cornelius".to_string(), },
];

let inserted_names = diesel::insert(&new_users)
    .into(users)
    .returning(name)
    .get_results(&connection);
assert_eq!(Ok(vec!["Diva Plavalaguna".to_string(), "Father Vito Cornelius".to_string()]), inserted_names);