Better compile error for entity without primary key (#1020)

This commit is contained in:
Billy Chan 2022-10-06 23:50:39 +08:00 committed by GitHub
parent 3a2d5168a8
commit 29deb0dfd1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 13 deletions

View File

@ -306,16 +306,15 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
} }
} }
let primary_key = (!primary_keys.is_empty()) let primary_key = {
.then(|| { let auto_increment = auto_increment && primary_keys.len() == 1;
let auto_increment = auto_increment && primary_keys.len() == 1; let primary_key_types = if primary_key_types.len() == 1 {
let primary_key_types = if primary_key_types.len() == 1 { let first = primary_key_types.first();
let first = primary_key_types.first(); quote! { #first }
quote! { #first } } else {
} else { quote! { (#primary_key_types) }
quote! { (#primary_key_types) } };
}; quote! {
quote! {
#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] #[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
pub enum PrimaryKey { pub enum PrimaryKey {
#primary_keys #primary_keys
@ -329,9 +328,8 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
#auto_increment #auto_increment
} }
} }
} }
}) };
.unwrap_or_default();
Ok(quote! { Ok(quote! {
#[derive(Copy, Clone, Debug, sea_orm::prelude::EnumIter, sea_orm::prelude::DeriveColumn)] #[derive(Copy, Clone, Debug, sea_orm::prelude::EnumIter, sea_orm::prelude::DeriveColumn)]

View File

@ -14,6 +14,12 @@ pub fn expand_derive_primary_key(ident: Ident, data: Data) -> syn::Result<TokenS
} }
}; };
if variants.is_empty() {
return Ok(quote_spanned! {
ident.span() => compile_error!("Entity must have a primary key column. See <https://github.com/SeaQL/sea-orm/issues/485> for details.");
});
}
let variant: Vec<TokenStream> = variants let variant: Vec<TokenStream> = variants
.iter() .iter()
.map(|Variant { ident, fields, .. }| match fields { .map(|Variant { ident, fields, .. }| match fields {

View File

@ -108,6 +108,27 @@ pub fn derive_entity(input: TokenStream) -> TokenStream {
/// # /// #
/// # impl ActiveModelBehavior for ActiveModel {} /// # impl ActiveModelBehavior for ActiveModel {}
/// ``` /// ```
///
/// Entity should always have a primary key.
/// Or, it will result in a compile error.
/// See <https://github.com/SeaQL/sea-orm/issues/485> 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))] #[proc_macro_derive(DeriveEntityModel, attributes(sea_orm))]
pub fn derive_entity_model(input: TokenStream) -> TokenStream { pub fn derive_entity_model(input: TokenStream) -> TokenStream {
let input_ts = input.clone(); let input_ts = input.clone();