Skip to content

Feature request: Add support for conditional cfg_attr for primary key #215

Open
@nikessel

Description

@nikessel

I have a use case where I need to conditionally add the proc macro attributes to a model based on a feature. This compiles:

#[cfg_attr(
feature = "ssr",
native_db,
native_model(id = 1, version = 1)
)]
#[derive(Clone, Debug, Deserialize, Serialize, Getters, Dissolve, new)]
pub struct User {
#[primary_key]
// #[cfg_attr(feature = "ssr", primary_key)]
pub id: IdType,
}

However, if I comment in the #[cfg_attr(feature = "ssr", primary_key)], the proc macro panics with: "Primary key is not set", even though native_db is only added to the model if "ssr" is enabled.

I would have created a pull request, but proc macros are a bit beyond my ability. For now I've forked the project and added a workaround:

pub(crate) fn primary_key(&self) -> KeyDefinition<()> {
    // self.primary_key.clone().expect("Primary key is not set")
    self.primary_key.clone().unwrap_or_else(|| {
        KeyDefinition::new_field(
            self.struct_name.clone(),
            Ident::new("id", Span::call_site()),
            (),
        )
    })
}

Metadata

Metadata

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions