sea-orm icon indicating copy to clipboard operation
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

Open langzime opened this issue 2 years ago • 13 comments

Motivation

Proposed Solutions

Additional Information

langzime avatar Jun 06 '22 02:06 langzime

Hey @langzime, is this a follow up question of

  • https://github.com/SeaQL/sea-orm/issues/782

billy1624 avatar Jun 06 '22 02:06 billy1624

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.

billy1624 avatar Jun 06 '22 02:06 billy1624

Yes, i tried it. A lot of macros cause ide very laggy and a lot of extra code

langzime avatar Jun 06 '22 03:06 langzime

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.

billy1624 avatar Jun 06 '22 03:06 billy1624

I have 500+ ...

langzime avatar Jun 06 '22 03:06 langzime

In cases like this, I currently use both mysql_async

langzime avatar Jun 06 '22 03:06 langzime

I think there are three options:

  1. 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
  2. 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

billy1624 avatar Jun 06 '22 04:06 billy1624

Thank you very much, I'll try it sometime. Do you have plans to support this feature?

langzime avatar Jun 07 '22 04:06 langzime

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

billy1624 avatar Jun 07 '22 04:06 billy1624

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).

tyt2y3 avatar Jun 27 '22 16:06 tyt2y3

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

tyt2y3 avatar Jun 27 '22 16:06 tyt2y3

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

billy1624 avatar Jun 28 '22 04:06 billy1624

Then probably a lazy_static with Entity(&'static str) would work

tyt2y3 avatar Jun 30 '22 17:06 tyt2y3