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

Custom type for table primary key #1770

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

AngelOnFira
Copy link
Contributor

PR Info

This PR addresses ideas from #1310. It isn't complete, but I wanted to start by adding some implementation for my ideas. The idea of this feature is to provide a type unique for each primary key of each table. This will make it easier to pass around primary key variables and know what table they reference.

  • Dependencies:
    • will be dependant on a change in sea-query

New Features

This currently just adds a custom Rust type for compact entity generation. For example, this model:

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "challenge")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i32,
}

would become:

pub type ChallengeTablePK = i32; // <-- type defined here

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "challenge")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: ChallengeTablePK, // <-- id references type
}

Breaking Changes

This won't add any breaking changes as it will be behind a CLI argument, and will not be enabled by default.

Changes

  • Add CLI argument for adding custom types to table primary keys
  • Change how entity files are generated

TODO

There are several items that have not yet been implemented:

  • Adding this feature to expanded entity form
  • Figuring out how to handle multiple primary keys on a table
  • Verify this works correctly with queries
  • Complete testing for more complex types
  • Squash commits on this branch

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

Successfully merging this pull request may close these issues.

None yet

1 participant