kurtbuilds / ormlite

An ORM in Rust for developers that love SQL.
https://crates.io/crates/ormlite
MIT License
216 stars 11 forks source link

A way to trigger the creation of a different model on the creation of a model #49

Open Thermatix opened 3 months ago

Thermatix commented 3 months ago

So I have a User model, and an Access model, I want to trigger the creation of an Access model at the same time (with some reasonable defaults) as it's owning user model get's created.

like

#[derive(Model)]
#[ormlite(on_create(Create(Foo, foo)))] // some way to indicate model and join field
#[ormlite(on_delete(Delete(Foo, foo)))]
pub struct Foo {
    #[ormlite(primary_key)]
    pub id: ID,
}

#[derive(Model)]
pub struct Bar {
    #[ormlite(primary_key)]
    pub id: ID,
    #[ormlite(join_column = "foo_id")]
    pub foo: Join<Foo>,
}

The reason I suggest a separate on_create & Create is because maybe you want to do something else, maybe Delete or Trigger a function or something.

Only thing I'm not sure about are fields that have no defaults, perhaps just flat-out don't allow models like that to be auto-created

kurtbuilds commented 3 months ago

I think the straight forward way to do this is using database triggers (I'm most familiar with Postgres - less sure about others).

Right now ormlite doesn't include this, but I'd welcome any PRs to support it, and happy to answer any questions you encounter working on it.

Thermatix commented 1 day ago

How do I manage this manually?