Electron100 / butane

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

Persisting members #15

Closed janosimas closed 2 years ago

janosimas commented 2 years ago

I got a Type with a Many<OtherType> member but it doesn't persist the members.,

I tried also overloading the save method and calling save on the member but when I try to save it I'm getting the error: value: NotInitialized.

struct MyType {
  #[auto] id: i32,
  member: Many<OtherType>,
}

impl MyType {
  pub fn save(&mut self, conn: &Connection) -> Result<(), butane::Error> {
      self.member.save(conn)?;
      DataObject::save(self, conn)
  }
}
Electron100 commented 2 years ago

Hmm, that's surprising as there are tests that should be ensuring that save and load are working properly on Many. Are you able to provide an example source file/project that demonstrates the bug?

Which Many method are you calling to try to retrieve the values and what return are you getting?

janosimas commented 2 years ago

Let me clarify what I'm doing.

I'm creating a new MyType and in the initialization fill up the new members. After this I try to save MyType. Looks like here is my issue.

After this, I try to load with MyType::query().load(&conn).unwrap(); and I get an empty Many. I checked the database and the link table has owner = -1 and has = -1.

I uploaded the code I was working on: https://github.com/janosimas/code_as_db

You should be able to reproduce the issue with the current code from code_as_db_models/src/main.rs

Electron100 commented 2 years ago

Thanks. I took a look, and I see what's going on. There's actually two issues here

  1. There's was Butane bug that affects the first time an object with both an auto-pk and a Many field is saved (basically the Many was trying to refer to the pk before it was known). I've just released 0.4.2 which addresses this.
  2. Your Parameter objects were never saved by your code. The Many doesn't own these -- the add method only takes a reference. You should save them before adding them to the Many -- otherwise they don't exist in the database at all. I'll look into whether it makes sense to make add return an error for an object that has not yet been saved.
janosimas commented 2 years ago

Hey, thanks for for the quick responses for all my issues. 👍

I think an Error would be fine, it's better then a silent error.