Electron100 / butane

An ORM for Rust with a focus on simplicity and on writing Rust, not SQL
Apache License 2.0
94 stars 13 forks source link

save() silently disregards unsaved Many & ForeignKey relationships #109

Open jayvdb opened 1 year ago

jayvdb commented 1 year ago

If we remove the tag.save(..) at https://github.com/Electron100/butane/blob/master/butane/tests/common/blog.rs#L81 , the many.rs tests fail, but only fail at the assertions on the number of tags after the posts are re-loaded from the database.

Likewise removing the blog.save(..) at https://github.com/Electron100/butane/blob/35ef9d0/butane/tests/common/blog.rs#L91 , the query.rs test fkey_match fails, but it fails trying to fetch the blog, not during the storing of the post in the db.

The Post.save(..) should detect that the related tags are not saved, and either save them, or error. IMO doing the save is the most desirable, and isnt a breaking change as anyone who had code that triggers the related save's had broken code.

jayvdb commented 8 months ago

Due to https://github.com/Electron100/butane/pull/158 , postgres now catches this, resulting in

called `Result::unwrap()` on an `Err` value: Postgres(Error { kind: Db, cause: Some(DbError { severity: "ERROR", parsed_severity: Some(Error), code: SqlState(E23503), message: "insert or update on table \"bar\" violates foreign key constraint \"bar_foo_fkey\"", detail: Some("Key (foo)=(1) is not present in table \"foo\"."), hint: None, position: None, where_: None, schema: Some("public"), table: Some("bar"), column: None, datatype: None, constraint: Some("bar_foo_fkey"), file: Some("ri_triggers.c"), line: Some(2608), routine: Some("ri_ReportViolation") }) })

sqlite ignores it.