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

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

Open
Thermatix opened this issue Apr 18, 2024 · 2 comments

Comments

@Thermatix
Copy link

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
Copy link
Owner

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
Copy link
Author

How do I manage this manually?

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

2 participants