sea-orm
sea-orm copied to clipboard
Dynamic Table Name, a lot of tables with the same structure, it is not appropriate to define many structures with macros
Motivation
Proposed Solutions
Additional Information
Hey @langzime, is this a follow up question of
- https://github.com/SeaQL/sea-orm/issues/782
Yeah, this is the tradeoff. Defining the repeated structs over and over again OR defining repeated struct with macro. Define with macro will improve the readability and avoid repeated code but it's harder to develop upon as not all IDEs are able to expand the macros.
Yes, i tried it. A lot of macros cause ide very laggy and a lot of extra code
From my experience, it's just a one-time long compilation as 200+ entities were added at once. Once it's compiled it will never be recompiled on the same machine given the fact that entity file is unchanged.
I have 500+ ...
In cases like this, I currently use both mysql_async
I think there are three options:
- Fork SeaORM and change this part, making it to be provided in runtime: https://github.com/SeaQL/sea-orm/blob/1ea8b457bf9f66988a45696c15933a6a756854ed/src/tests_cfg/cake_expanded.rs#L7-L11
- Construct SQL statement with SeaQuery then pass it to SeaORM for execution: https://github.com/SeaQL/starfish-ql/blob/459a71609785f74739d4b5ba9a2983af0cedfae6/starfish/starfish-core/src/query/mod.rs#L290-L303
Thank you very much, I'll try it sometime. Do you have plans to support this feature?
Do you have plans to support this feature?
It's not on our plan. But if you have an good proposal we're willing to consider :D
Actually, it's possible to have an Entity that has a state, so your Entity is no longer a unit struct, but a struct Entity(String)
.
Although I am not sure are there other blockers to this approach. But yeah at least it was designed that way, the table_name
takes a self
From table_name
perspective, it's designed with runtime in mind. https://github.com/SeaQL/sea-orm/blob/6bc76698cc54aea1eb54cbd82d1d5af096432079/src/entity/base_entity.rs#L23-L24
However, when constructing query. It doesn't take &self
. So, it's solely rely on the Entity
defined in compile-time.
Also, EntityName
must be Copy
but String
isn't. So, pub struct Entity(pub String)
wouldn't compile.
https://github.com/SeaQL/sea-orm/blob/6bc76698cc54aea1eb54cbd82d1d5af096432079/src/entity/base_entity.rs#L10-L24
Then probably a lazy_static with Entity(&'static str)
would work