From 29deb0dfd1c3a6b0092727971d5161c1a4a88642 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Thu, 6 Oct 2022 23:50:39 +0800 Subject: [PATCH] Better compile error for entity without primary key (#1020) --- sea-orm-macros/src/derives/entity_model.rs | 24 ++++++++++------------ sea-orm-macros/src/derives/primary_key.rs | 6 ++++++ sea-orm-macros/src/lib.rs | 21 +++++++++++++++++++ 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/sea-orm-macros/src/derives/entity_model.rs b/sea-orm-macros/src/derives/entity_model.rs index 61a45a43..193e6e99 100644 --- a/sea-orm-macros/src/derives/entity_model.rs +++ b/sea-orm-macros/src/derives/entity_model.rs @@ -306,16 +306,15 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec) -> syn::Res } } - let primary_key = (!primary_keys.is_empty()) - .then(|| { - let auto_increment = auto_increment && primary_keys.len() == 1; - let primary_key_types = if primary_key_types.len() == 1 { - let first = primary_key_types.first(); - quote! { #first } - } else { - quote! { (#primary_key_types) } - }; - quote! { + let primary_key = { + let auto_increment = auto_increment && primary_keys.len() == 1; + let primary_key_types = if primary_key_types.len() == 1 { + let first = primary_key_types.first(); + quote! { #first } + } else { + quote! { (#primary_key_types) } + }; + quote! { #[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] pub enum PrimaryKey { #primary_keys @@ -329,9 +328,8 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec) -> syn::Res #auto_increment } } - } - }) - .unwrap_or_default(); + } + }; Ok(quote! { #[derive(Copy, Clone, Debug, sea_orm::prelude::EnumIter, sea_orm::prelude::DeriveColumn)] diff --git a/sea-orm-macros/src/derives/primary_key.rs b/sea-orm-macros/src/derives/primary_key.rs index 3a05617b..d5c86c96 100644 --- a/sea-orm-macros/src/derives/primary_key.rs +++ b/sea-orm-macros/src/derives/primary_key.rs @@ -14,6 +14,12 @@ pub fn expand_derive_primary_key(ident: Ident, data: Data) -> syn::Result compile_error!("Entity must have a primary key column. See for details."); + }); + } + let variant: Vec = variants .iter() .map(|Variant { ident, fields, .. }| match fields { diff --git a/sea-orm-macros/src/lib.rs b/sea-orm-macros/src/lib.rs index d3026d23..ef77c1e4 100644 --- a/sea-orm-macros/src/lib.rs +++ b/sea-orm-macros/src/lib.rs @@ -108,6 +108,27 @@ pub fn derive_entity(input: TokenStream) -> TokenStream { /// # /// # impl ActiveModelBehavior for ActiveModel {} /// ``` +/// +/// Entity should always have a primary key. +/// Or, it will result in a compile error. +/// See for details. +/// +/// ```compile_fail +/// use sea_orm::entity::prelude::*; +/// +/// #[derive(Clone, Debug, PartialEq, DeriveEntityModel)] +/// #[sea_orm(table_name = "posts")] +/// pub struct Model { +/// pub title: String, +/// #[sea_orm(column_type = "Text")] +/// pub text: String, +/// } +/// +/// # #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +/// # pub enum Relation {} +/// # +/// # impl ActiveModelBehavior for ActiveModel {} +/// ``` #[proc_macro_derive(DeriveEntityModel, attributes(sea_orm))] pub fn derive_entity_model(input: TokenStream) -> TokenStream { let input_ts = input.clone();