diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d4a4a269e..c68a43102c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ Increasing the minimal supported Rust version will always be coupled at least wi * Added automatic usage of all sqlite `rowid` aliases when no explicit primary key is defined for `print-schema` * Added a `#[dsl::auto_type]` attribute macro, allowing to infer type of query fragment functions * Added the same type inference on `Selectable` derives, which allows skipping specifying `select_expression_type` most of the time, in turn enabling most queries to be written using just a `Selectable` derive. +* Added an optional `#[diesel(skip_insertion)]` field attribute to the `Insertable` derive macro, allowing fields which map to generated columns to be skipped during insertion. ### Changed diff --git a/diesel_derives/src/lib.rs b/diesel_derives/src/lib.rs index 7f4d95fdf7..b1dceb5f86 100644 --- a/diesel_derives/src/lib.rs +++ b/diesel_derives/src/lib.rs @@ -334,6 +334,8 @@ pub fn derive_identifiable(input: TokenStream) -> TokenStream { /// the actual field type. /// * `#[diesel(treat_none_as_default_value = true/false)]`, overrides the container-level /// `treat_none_as_default_value` attribute for the current field. +/// * `#[diesel(skip_insertion)]`, skips insertion of this field. Useful for working with +/// generated columns. /// /// # Examples /// diff --git a/diesel_tests/tests/insert.rs b/diesel_tests/tests/insert.rs index 5ec66394d3..410d6174f1 100644 --- a/diesel_tests/tests/insert.rs +++ b/diesel_tests/tests/insert.rs @@ -469,7 +469,7 @@ fn insert_with_generated_column() { "CREATE TABLE user_with_last_names ( first_name VARCHAR NOT NULL PRIMARY KEY, last_name VARCHAR NOT NULL, - full_NAME VARCHAR GENERATED ALWAYS AS (first_name || ' ' || last_name) STORED + full_name VARCHAR GENERATED ALWAYS AS (first_name || ' ' || last_name) STORED )", ) .execute(connection) @@ -477,7 +477,7 @@ fn insert_with_generated_column() { let new_users: &[_] = &[UserWithLastName { first_name: "Sean".to_string(), last_name: "Black".to_string(), - ..Default::default() + full_name: "This field not inserted".to_string(), }]; let count = insert_into(users) .values(new_users)