Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update and set null: There are no changes to save. This query cannot be built #885

Closed
fluxxu opened this issue May 3, 2017 · 1 comment

Comments

@fluxxu
Copy link
Contributor

fluxxu commented May 3, 2017

Which versions of Rust and Diesel are you using?

rustc 1.19.0-nightly (06fb4d256 2017-04-30)
diesel 0.12

Which feature flags are you using?

postgres, chrono, serde

What are you trying to accomplish?

generate a query to update fields to null
e.g.

UPDATE "tablename" SET "value" = NULL WHERE "id" = ?

What is the full error you are seeing?

panicked at 'called `Result::unwrap()` on an `Err` value: Error(DbResult(QueryBuilderError(StringError("There are no changes to save. This query cannot be built"))), State { next_error: None, backtrace: None
 })', /checkout/src/libcore/result.rs:859

Because None is a noop:

And struct with only None values can not pass this:

How can we reproduce this?

define a struct with only Option fields and derive AsChangeset

  #[derive(AsChangeset)]
  #[table_name="tablename"]
  struct Update {
    value: Option<i32>,
  }

Then update using this struct

  diesel::update(tablename::table.filter(dsl::id.eq(id)))
    .set(&Update {
        value: None
    })
    .execute(conn)?;

Please quote as much of your code as needed to reproduce (best link to a public repository or a Gist)

N/A

Please post as much of your database schema as is relevant to your error

N/A

@fluxxu
Copy link
Contributor Author

fluxxu commented May 5, 2017

http://docs.diesel.rs/diesel/query_builder/trait.AsChangeset.html

By default, any Option fields on the struct are skipped if their value is None. If you would like to assign NULL to the field instead, you can annotate your struct with #[changeset_options(treat_none_as_null = "true")].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant