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 table names like user1, user2,user... , They all have same columns, but table name #782

Closed
langzime opened this issue Jun 2, 2022 · 3 comments

Comments

@langzime
Copy link

langzime commented Jun 2, 2022

Motivation

Proposed Solutions

Additional Information

@billy1624
Copy link
Member

Hey @langzime, welcome! If I understand your question correctly:

macro_rules! def_entity {
    ($mod: ident, $table_name: literal) => {
        pub mod $mod {
            use sea_orm::entity::prelude::*;
    
            #[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
            #[sea_orm(table_name = $table_name)]
            pub struct Model {
                #[sea_orm(primary_key)]
                pub id: i32,
            }
    
            #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
            pub enum Relation {}
    
            impl ActiveModelBehavior for ActiveModel {}
        }
    };
}

def_entity!(user, "user");
def_entity!(user1, "user1");
def_entity!(user2, "user2");

You can create a macro your own to define those entities with ease.

@langzime
Copy link
Author

langzime commented Jun 5, 2022

Hey @langzime, welcome! If I understand your question correctly:

macro_rules! def_entity {
    ($mod: ident, $table_name: literal) => {
        pub mod $mod {
            use sea_orm::entity::prelude::*;
    
            #[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
            #[sea_orm(table_name = $table_name)]
            pub struct Model {
                #[sea_orm(primary_key)]
                pub id: i32,
            }
    
            #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
            pub enum Relation {}
    
            impl ActiveModelBehavior for ActiveModel {}
        }
    };
}

def_entity!(user, "user");
def_entity!(user1, "user1");
def_entity!(user2, "user2");

You can create a macro your own to define those entities with ease.

but it adds a lot of repetition,

@langzime
Copy link
Author

langzime commented Jun 5, 2022

Hey @langzime, welcome! If I understand your question correctly:

macro_rules! def_entity {
    ($mod: ident, $table_name: literal) => {
        pub mod $mod {
            use sea_orm::entity::prelude::*;
    
            #[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
            #[sea_orm(table_name = $table_name)]
            pub struct Model {
                #[sea_orm(primary_key)]
                pub id: i32,
            }
    
            #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
            pub enum Relation {}
    
            impl ActiveModelBehavior for ActiveModel {}
        }
    };
}

def_entity!(user, "user");
def_entity!(user1, "user1");
def_entity!(user2, "user2");

You can create a macro your own to define those entities with ease.

I have 256 tables and other 128 tables

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

No branches or pull requests

2 participants