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

Implement support for single PK field in ModelV2 #8111

Open
leovalais opened this issue Jul 18, 2024 · 0 comments
Open

Implement support for single PK field in ModelV2 #8111

leovalais opened this issue Jul 18, 2024 · 0 comments
Labels

Comments

@leovalais
Copy link
Contributor

Description and goal

We want the following code to work:

#[derive(Debug, Clone, ModelV2)]
#[derive(table = XYZ)]
struct MyModel {
     id: i64
}

It currently fails because the macro generate the following changeset definition:

#[derive(Debug, Default, Queryable, AsChangeset, Insertable)]
#[diesel(table_name = XYZ)]
struct MyModelChangeset {}

But an empty struct is not supported by diesel's AsChangeset and Insertable.

Sadly, the current trait Model definition requires every model to have a changeset type implementing AsChangeset (and should also require Insertable tbh).

🔧 Suggested fix

  1. Complete and merge Add codegen filter to ModelV2 #7898
  2. Relax the bound AsChangeset of trait Model. We can do this since the queries are generated by the macro and we don't rely on generic type parameters. So this bound is not strictly necessary.
  3. Generate and empty changeset struct that doesn't derive(AsChangeset, Insertable).
  4. Generate a different implementation for create and update operations if the model that's being expanded contains no non-PK field.
  • create and create_batch could use .default_values() expression method
  • updates don't need to be implemented as PK changes is out of ModelV2's scope
  1. If derive(ModelV2) is applied to a struct that only contains a PK, the macro can change its ImplPlan to avoid generating the Update* impl blocks. If those were asked explicitly, an error should be raised.

Acceptance criteria

#[derive(Debug, Clone, ModelV2)]
#[derive(table = XYZ)]
struct MyModel {
     id: i64
}

compiles and works as expected

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

No branches or pull requests

2 participants